Unity VS脚本自动添加头部注释

2020-11-21  本文已影响0人  菜鸟的笔记

1.首先找到Unity的安装目录下文件夹ScriptTemplates:
Windows地址 D:\Program Files\Unity\Editor\Data\Resources\ScriptTemplates
Mac地址: /Applications/Unity/Unity.app/Contents/Resources/ScriptTemplates
Unity中的脚本创建使用的是模板拷贝,拷贝的就是这个文件夹下的脚本。
2.打开里面的 81-C# Script-NewBehaviourScript.cs,这个是创建C#脚本的模板,在里面添加注释内容:

/**
 *Copyright(C) 2015 by #COMPANY#
 *All rights reserved.
 *FileName:     #SCRIPTFULLNAME#
 *Author:       #AUTHOR#
 *Version:      #VERSION#
 *UnityVersion:#UNITYVERSION#
 *Date:         #DATE#
 *Description:   
 *History:
*/
using UnityEngine;
using System.Collections;
 
public class #SCRIPTNAME# : MonoBehaviour {
 
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}

3.修改后保存,然后进入Unit编辑器,在Editor文件夹下创建脚本:AddFileHeadComment.cs

using UnityEditor;
using UnityEngine;
using System.IO;
 
public class AddFileHeadComment : UnityEditor.AssetModificationProcessor
{
    /// <summary>
    /// 此函数在asset被创建完,文件已经生成到磁盘上,但是没有生成.meta文件和import之前被调用
    /// </summary>
    /// <param name="newFileMeta">newfilemeta 是由创建文件的path加上.meta组成的</param>
    public static void OnWillCreateAsset(string newFileMeta)
    {
        string newFilePath = newFileMeta.Replace(".meta", "");
        string fileExt = Path.GetExtension(newFilePath);
        if (fileExt != ".cs")
        {
            return;
        }
        //注意,Application.datapath会根据使用平台不同而不同
        string realPath = Application.dataPath.Replace("Assets", "") + newFilePath;
        string scriptContent = File.ReadAllText(realPath);
 
        //这里实现自定义的一些规则
        scriptContent = scriptContent.Replace("#SCRIPTFULLNAME#", Path.GetFileName(newFilePath));
        scriptContent = scriptContent.Replace("#COMPANY#", PlayerSettings.companyName);
        scriptContent = scriptContent.Replace("#AUTHOR#", "Passion");
        scriptContent = scriptContent.Replace("#VERSION#", "1.0");
        scriptContent = scriptContent.Replace("#UNITYVERSION#", Application.unityVersion);
        scriptContent = scriptContent.Replace("#DATE#", System.DateTime.Now.ToString("yyyy-MM-dd"));
 
        File.WriteAllText(realPath, scriptContent);
    }
 
}

4.保存脚本,脚本使用的原理就是在Unity保存脚本的时候对##的关键字进行真实信息的替换,达到时间和公司,作者等信息的准确自定义。
5.OK,右键创建一个脚本试一下吧!

转载地址,点击查看原文

上一篇下一篇

猜你喜欢

热点阅读