1. 程式人生 > >Unity訂製新建指令碼模板

Unity訂製新建指令碼模板

模板檔案位於Unity安裝目錄Unity5.4.2f2\Editor\Data\Resources\ScriptTemplates\下:/81-C# Script-NewBehaviourScript.cs.txt

我們可以手動修改該檔案,新增自己需要的key,然後新增解析對應key的指令碼

如下為解析模板指令碼的原始碼,需要放在unity工程下的Assets/Editor目錄下

using UnityEngine;

using UnityEditor;
using System.IO;

public class BespokeScriptTemplate : AssetModificationProcessor
{
    private static void OnWillCreateAsset(string path)
    {
        path = path.Replace(".meta", "");
        int index = path.LastIndexOf(".");
        string file = path.Substring(index);
        if (file != ".cs" && file != ".js" && file != ".boo") return;
        string fileExtension = file;
        index = Application.dataPath.LastIndexOf("Assets");
        path = Application.dataPath.Substring(0, index) + path;
        file = File.ReadAllText(path);
        file = file.Replace("#CREATIONDATE#", System.DateTime.Now.ToString("d"));
        file = file.Replace("#PROJECTNAME#", PlayerSettings.productName);
        file = file.Replace("#SMARTDEVELOPERS#", PlayerSettings.companyName);
        file = file.Replace("#FILEEXTENSION#", fileExtension);
        File.WriteAllText(path, file);
        AssetDatabase.Refresh();
    }
}