简单来说就是在创建脚本的时候,得.cs脚本路径,然后读出这个脚本所有的内容并且修改。代码如下:
using UnityEditor; using UnityEngine; using System.IO; public class AddFileHeadComment : UnityEditor.AssetModificationProcessor { // 添加脚本注释模板 private static string str = "/** " + "*Copyright(C) 2017 by #COMPANY# " + "*All rights reserved. " + "*ProductName: #PRODUCTNAME# " + "*Author: #AUTHOR# " + "*Version: #VERSION# " + "*UnityVersion: #UNITYVERSION# " + "*CreateTime: #CreateTime# " + "*Description: " + "*/ "; /// <summary> /// 此函数在asset被创建完,文件已经生成到磁盘上,但是没有生成.meta文件和import之前被调用 /// </summary> /// <param name="newFileMeta">newfilemeta 是由创建文件的path加上.meta组成的</param> public static void OnWillCreateAsset(string newFileMeta) { // 只修改C#脚本 string newFilePath = newFileMeta.Replace(".meta", ""); if (newFilePath.EndsWith(".cs")) { string scriptContent = str; scriptContent += File.ReadAllText(newFilePath); // 替换字符串为系统时间 scriptContent = scriptContent.Replace("#CreateTime#", System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); //这里实现自定义的一些规则 scriptContent = scriptContent.Replace("#SCRIPTFULLNAME#", Path.GetFileName(newFilePath)); scriptContent = scriptContent.Replace("#COMPANY#", PlayerSettings.companyName); scriptContent = scriptContent.Replace("#PRODUCTNAME#", PlayerSettings.productName); scriptContent = scriptContent.Replace("#AUTHOR#", "CW"); scriptContent = scriptContent.Replace("#VERSION#", "1.0"); scriptContent = scriptContent.Replace("#UNITYVERSION#", Application.unityVersion); File.WriteAllText(newFilePath, scriptContent); } } }
测试:
/** *Copyright(C) 2017 by DefaultCompany *All rights reserved. *ProductName: SteamPro *Author: CW *Version: 1.0 *UnityVersion: 5.3.5f1 *CreateTime: 2017/12/11 14:08:28 *Description: */ using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } }