Unity VS脚本自动添加头部注释
http://blog.****.net/yupu56/article/details/52326930
很多人一起可能都使用过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,右键创建一个脚本试一下吧!