示例:
直接上脚本,放入Editor文件夹下即可:
using System;
using System.IO;
using UnityEngine;
/// <summary>
/// 设置自定义脚本模板代码
/// </summary>
public class ScriptHeadComment : UnityEditor.AssetModificationProcessor
{
public static void OnWillCreateAsset(string metaName)
{
string filePath = metaName.Replace(".meta", "");
string fileExt = Path.GetExtension(filePath);
if (fileExt != ".cs")
{
return; // 不是脚本
}
string fileFullPath = Application.dataPath.Replace("Assets", "") + filePath;
string fileContent = File.ReadAllText(fileFullPath);
// 按照自己的设计添加需要自动生成的信息,调整好间距
string commentContent =
"/*\n *FileName: #FILENAME#\n *Author: #AUTHOR#\n *Date: #DATE#\n *UnityVersion: #UNITYVERSION#\n *Description:\n*/\n";
commentContent = commentContent.Replace("#FILENAME#", Path.GetFileName(fileFullPath));
commentContent = commentContent.Replace("#AUTHOR#", "AUTHOR");
commentContent = commentContent.Replace("#DATE#", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
commentContent = commentContent.Replace("#UNITYVERSION#", Application.unityVersion);
fileContent = fileContent.Insert(0, commentContent);
File.WriteAllText(fileFullPath, fileContent);
}
}