Unity订制新建脚本模板

来源:互联网 发布:java时间排序函数 编辑:程序博客网 时间:2024/05/16 19:25

模板文件位于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();
    }
}
0 0