Last active
November 7, 2019 19:21
-
-
Save starikcetin/ae9cf662eb8d201f0d0972e858c04a1f to your computer and use it in GitHub Desktop.
NamespaceFillerAssetProcessor : Fills in #NAMESPACE# variables in Unity script templates. Source: https://stackoverflow.com/a/52395369/6301627
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEditor; | |
using UnityEngine; | |
public class NamespaceFillerAssetProcessor : UnityEditor.AssetModificationProcessor | |
{ | |
public static void OnWillCreateAsset(string path) | |
{ | |
path = path.Replace(".meta", ""); | |
var index = path.LastIndexOf(".", StringComparison.Ordinal); | |
if (index < 0) | |
{ | |
return; | |
} | |
var file = path.Substring(index); | |
if (file != ".cs" && file != ".js" && file != ".boo") | |
{ | |
return; | |
} | |
index = Application.dataPath.LastIndexOf("Assets", StringComparison.Ordinal); | |
path = Application.dataPath.Substring(0, index) + path; | |
file = System.IO.File.ReadAllText(path); | |
var lastPart = path.Substring(path.IndexOf("Assets", StringComparison.Ordinal)); | |
var _namespace = lastPart.Substring(0, lastPart.LastIndexOf('/')); | |
_namespace = _namespace.Replace('/', '.'); | |
file = file.Replace("#NAMESPACE#", _namespace); | |
System.IO.File.WriteAllText(path, file); | |
AssetDatabase.Refresh(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment