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

很多人一起可能都使用过VS或者Eclipse之类的IDE来开发,都使用过在脚本的头部添加注释,标注时间,作者,修改等等的信息。
那么在Unity中使用MonoBehavior或者VS的时候怎么来实现这个功能呢?
先展示下效果:


下面说明下实现原理:
1.首先找到Unity的安装目录下文件夹ScriptTemplates:D:\Program Files\Unity\Editor\Data\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,右键创建一个脚本试一下吧!


补充:Mac版本的Unity 地址是:

 /Applications/Unity/Unity.app/Contents/Resources/ScriptTemplates/81-C\#\ Script-NewBehaviourScript.cs.txt 


  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TxNet.Ltd.

你的赞同是对我的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值