Created
June 16, 2020 19:56
-
-
Save jonHuffman/bb9f996bd3cd8a69df50805c736aa9ef to your computer and use it in GitHub Desktop.
A Keyword processor for Unity script templates. Allows one to replace pre-defined symbols in a script template with procedurally determined values.
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
// Original gist by JoaoBorks, but the gist is no longer available | |
using UnityEngine; | |
using UnityEditor; | |
internal sealed class ScriptKeywordProcessor : UnityEditor.AssetModificationProcessor | |
{ | |
private const string ASSETS_FOLDER = "Assets"; | |
private const string SCRIPTS_FOLDER = "Scripts/"; | |
public static void OnWillCreateAsset(string path) | |
{ | |
path = path.Replace(".meta", ""); | |
int index = path.LastIndexOf("."); | |
if (index < 0) | |
{ | |
return; | |
} | |
string file = path.Substring(index); | |
if (file != ".cs" && file != ".js") | |
{ | |
return; | |
} | |
index = Application.dataPath.LastIndexOf(ASSETS_FOLDER); | |
path = Application.dataPath.Substring(0, index) + path; | |
if (!System.IO.File.Exists(path)) | |
{ | |
return; | |
} | |
string fileContent = System.IO.File.ReadAllText(path); | |
string author = System.Security.Principal.WindowsIdentity.GetCurrent().Name; | |
author = author.Contains("\\") ? author.Split('\\')[1] : author; | |
// At this part you could actually get the name from Windows user directly or give it whatever you want | |
fileContent = fileContent.Replace("#AUTHOR#", System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\')[1]); | |
fileContent = fileContent.Replace("#CREATIONDATE#", System.DateTime.Now.ToString("dd/MM/yy")); | |
fileContent = fileContent.Replace("#NAMESPACE#", GetNamespace(path)); | |
System.IO.File.WriteAllText(path, fileContent); | |
AssetDatabase.Refresh(); | |
} | |
private static string GetNamespace(string path) | |
{ | |
path = path.Replace(".meta", ""); | |
string rootNamespace = GetRootNamespace(); | |
int index = path.LastIndexOf("."); | |
if (index < 0) | |
{ | |
return rootNamespace; | |
} | |
string lastPart = path.Substring(path.IndexOf(ASSETS_FOLDER)); | |
string _namespace = lastPart.Substring(0, lastPart.LastIndexOf('/')); | |
_namespace = _namespace.Replace(ASSETS_FOLDER, ""); | |
_namespace = _namespace.Replace(SCRIPTS_FOLDER, ""); | |
_namespace = _namespace.Replace('/', '.'); | |
if (!string.IsNullOrEmpty(_namespace)) | |
{ | |
_namespace = string.Join("", rootNamespace, _namespace); | |
} | |
return _namespace; | |
} | |
private static string GetRootNamespace() | |
{ | |
if (string.IsNullOrEmpty(EditorSettings.projectGenerationRootNamespace)) | |
{ | |
Debug.LogWarning("No Root Namespace defined in Editor settings, using Project name"); | |
return Application.productName; | |
} | |
return EditorSettings.projectGenerationRootNamespace; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment