Created
June 23, 2020 15:17
-
-
Save hadashiA/8403c391a229c3516471d05991406349 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
public sealed class ScriptTemplateProcessor : UnityEditor.AssetModificationProcessor | |
{ | |
const string MonoInstallerTemplate = | |
"using VContainer;\n" + | |
"using VContainer.Unity;\n" + | |
"\n" + | |
"public sealed class #SCRIPTNAME# : MonoInstaller\n" + | |
"{\n" + | |
" public override void Install(UnityContainerBuilder builder)\n" + | |
" {\n" + | |
" }\n" + | |
"}\n"; | |
public static void OnWillCreateAsset(string metaPath) | |
{ | |
var suffixIndex = metaPath.LastIndexOf(".meta"); | |
if (suffixIndex < 0) | |
{ | |
return; | |
} | |
var scriptPath = metaPath.Substring(0, suffixIndex); | |
var basename = Path.GetFileNameWithoutExtension(scriptPath); | |
var extname = Path.GetExtension(scriptPath); | |
if (extname != ".cs") | |
{ | |
return; | |
} | |
if (scriptPath.StartsWith("Assets/")) | |
{ | |
scriptPath = scriptPath.Substring("Assets/".Length); | |
} | |
var fullPath = Path.Combine(Application.dataPath, scriptPath); | |
if (scriptPath.EndsWith("Installer.cs")) | |
{ | |
var content = MonoInstallerTemplate.Replace("#SCRIPTNAME#", basename); | |
System.IO.File.WriteAllText(fullPath, content); | |
AssetDatabase.Refresh(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment