Unity 新创建的脚本自动添加模板注释(头部注释)

unity创建脚本时,自动添加头部注释
好处:
1.看着正规
2.解决了脚本的编码问题,默认的编码是GBK,中文注释,在unity的Inspector会显示乱码, 使用模板注释创建的脚本编码格式是utf-8的编码

代码:

using System.IO;
using UnityEngine;

	/// <summary>
    /// 新创建的脚本自动添加模板注释(头部注释)
    /// </summary>
    public class ScriptTemplate_editor : UnityEditor.AssetModificationProcessor
    {
    
    
        //作者
        private const string Author = "";

        /// <summary>
        /// 资源创建时调用
        /// </summary>
        /// <param name="path">自动传入资源路径</param>
        public static void OnWillCreateAsset(string path)
        {
    
    
            path = path.Replace(".meta", "");
            if (!path.EndsWith(".cs")) return;
            //注意,Application.datapath会根据使用平台不同而不同
            string realPath = Application.dataPath.Replace("Assets", "") + path;
            string allText = "/* =======================================================\r\n"
                           + " *  Unity版本:#UnityVersion#\r\n\r\n"
                           + " *  作 者:#Author#\r\n"
                           + " *  主 题:\r\n"
                           + " *  主要功能:\r\n\r\n"
                           + " *  详细描述:\r\n\r\n"
                           + " *  创建时间:#CreateTime#\r\n"
                           + " *  修改记录:\r\n"
                           + " *  版 本:1.0\r\n"
                           + " * =======================================================*/\r\n";
            allText += File.ReadAllText(realPath);
            allText = allText.Replace("#UnityVersion#", Application.unityVersion);
            allText = allText.Replace("#Author#", Author);
            allText = allText.Replace("#CreateTime#", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            File.WriteAllText(realPath, allText);
        }
    }

效果图:
在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44238530/article/details/128139853