ccruiの博客

ccruiの博客

Unity自定义新建脚本模板

68
2021-12-27

Unity自定义新建脚本模板

更改模板

更改Unity安装路径中的模板文件Unity\2020.3.18f1c1\Editor\Data\Resources\ScriptTemplates\81-C# Script-NewBehaviourScript.cs.txt,根据需要更改内容

/*****************************************

    文件:#SCRIPTNAME#.cs
    作者:
    邮箱:
    日期:#CreateTime#
    功能:Nothing

******************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    #ROOTNAMESPACEBEGIN#
public class #SCRIPTNAME# : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        #NOTRIM#
    }

    // Update is called once per frame
    void Update()
    {
        #NOTRIM#
    }
}
#ROOTNAMESPACEEND#

自动代码替换变量

在Unity工程中创建Plugins/Editor文件夹,将ScriptsInfoRecoder.cs脚本放入该文件夹即可

using System;
using System.IO;
 
public class ScriptsInfoRecoder : UnityEditor.AssetModificationProcessor {
    private static void OnWillCreateAsset(string path) {
        path = path.Replace(".meta", "");
        if (path.EndsWith(".cs")) {
            string str = File.ReadAllText(path);
            str = str.Replace("#CreateAuthor#", Environment.UserName).Replace(
                              "#CreateTime#", string.Concat(DateTime.Now.Year, "/", DateTime.Now.Month, "/",
                                DateTime.Now.Day, " ", DateTime.Now.Hour, ":", DateTime.Now.Minute, ":", DateTime.Now.Second));
            File.WriteAllText(path, str);
        }
    }
}

这时候在Unity工程中创建脚本都可以自动创建头部格式