簡單來說就是在建立腳本的時候,得.cs腳本路徑,然後讀出這個腳本所有的内容并且修改。代碼如下:
using UnityEditor;
using UnityEngine;
using System.IO;
public class AddFileHeadComment : UnityEditor.AssetModificationProcessor
{
// 添加腳本注釋模闆
private static string str =
"/**\r\n"
+ "*Copyright(C) 2017 by #COMPANY#\r\n"
+ "*All rights reserved.\r\n"
+ "*ProductName: #PRODUCTNAME#\r\n"
+ "*Author: #AUTHOR#\r\n"
+ "*Version: #VERSION#\r\n"
+ "*UnityVersion: #UNITYVERSION#\r\n"
+ "*CreateTime: #CreateTime#\r\n"
+ "*Description: \r\n"
+ "*/\r\n";
/// <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 () {
}
}
轉載于:https://www.cnblogs.com/weiqiangwaideshijie/p/8022870.html