Created
March 27, 2018 10:22
-
-
Save MewSoul/23c91e90d9c34c766c6a9d1f1d9ce9d7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 UnityEditor; | |
using UnityEngine; | |
namespace Utils { | |
public class ScriptTemplateModifier : UnityEditor.AssetModificationProcessor { | |
private const string SCRIPT_LOCATION = "Assets/Scripts/"; | |
private const string PROJECT_NAME = "#PROJECT_NAME#"; | |
private const string FILE_PATH = "#FILE_PATH#"; | |
#region CORE REPLACEMENT METHODS | |
public static void OnWillCreateAsset(string relativePath) { | |
relativePath = relativePath.Replace(".meta", ""); | |
int index = relativePath.LastIndexOf('.'); | |
if (index < 0) | |
return; | |
string extension = relativePath.Substring(index); | |
if (extension != ".cs" && extension != ".js" && extension != ".boo") | |
return; | |
index = Application.dataPath.LastIndexOf("Assets"); | |
string fullPath = Application.dataPath.Substring(0, index) + relativePath; | |
if (!System.IO.File.Exists(fullPath)) | |
return; | |
ReplaceKeywords(fullPath, relativePath); | |
AssetDatabase.Refresh(); | |
} | |
private static void ReplaceKeywords(string fullPath, string relativePath) { | |
string file = System.IO.File.ReadAllText(fullPath); | |
file = file.Replace(PROJECT_NAME, GetProjectName()); | |
file = file.Replace(FILE_PATH, GetPathNamespace(relativePath)); | |
System.IO.File.WriteAllText(fullPath, file); | |
} | |
#endregion | |
#region INFO METHODS | |
private static string GetProjectName() { | |
return Application.productName; | |
} | |
// ie: If the relative path is: "Assets/Scripts/Gameplay/Tiles/CustomTile.cs" | |
// It will return: "Gameplay.Tiles" | |
private static string GetPathNamespace(string relativePath) { | |
string pathNamespace = relativePath.Remove(0, SCRIPT_LOCATION.Length); | |
int index = pathNamespace.LastIndexOf('/'); | |
if (index < 0) | |
return ""; | |
pathNamespace = pathNamespace.Substring(0, pathNamespace.LastIndexOf('/')); | |
pathNamespace = pathNamespace.Replace('/', '.'); | |
return "." + pathNamespace; | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment