UnityEditor里中的類AssetModificationProcessor可以用來監(jiān)聽Project視圖中資源的創(chuàng)建、刪除攻旦、移動(dòng)、保存,可以把如下腳本放在Editor目錄中對(duì)新建腳本開發(fā)自動(dòng)添加模板 注釋抛姑。
using UnityEngine;
using UnityEditor;
using System.IO;
public class AddHeadComment : UnityEditor.AssetModificationProcessor
{
/// <summary>
/// asset已經(jīng)被創(chuàng)建完,文件已經(jīng)生成到磁盤上艳狐,但是沒有生成.meta文件和import之前被調(diào)用
/// </summary>
/// <param name="path">path的值是文件的路徑+ .meta</param>
public static void OnWillCreateAsset(string path)
{
string filePath = path.Replace(".meta", "");
string fileExtension = Path.GetExtension(filePath);
if (fileExtension != ".cs")
{
return;
}
string realPath = Application.dataPath.Replace("Assets", "") + filePath;
string scriptContent = File.ReadAllText(realPath);
string headComment = "/**\r\n *Copyright(C) 2018 by #COMPANY#\r\n *All rights reserved.\r\n *FileName: #SCRIPTFULLNAME#\r\n *Author: #AUTHOR# \r\n *Description:\r\n *Date: #DATE#\r\n */\r\n";
headComment = headComment.Replace("#COMPANY#", PlayerSettings.companyName);
headComment = headComment.Replace("#SCRIPTFULLNAME#", Path.GetFileName(filePath));
headComment = headComment.Replace("#AUTHOR#", System.Environment.UserName);
headComment = headComment.Replace("#DATE#", System.DateTime.Now.ToString("yyyy-mm-dd"));
scriptContent = scriptContent.Insert(0, headComment);
File.WriteAllText(realPath, scriptContent);
}
}