效果图
制作
在Editor目录下新建脚本,我这里取名为ScriptHeadComment
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class ScriptHeadComment : UnityEditor.AssetModificationProcessor
{
public static void OnWillCreateAsset(string metaName)
{
string filePath = metaName.Replace(".meta", ""); // "Assets/Scripts/ScriptHeadCommentTest.cs"
string fileExt = Path.GetExtension(filePath); // ".cs"
if (fileExt != ".cs")
{
return; // 不是脚本
}
string fileFullPath = Application.dataPath.Replace("Assets", "") + filePath; // "D:/Program Files/UnityProjects/UnityTest/Assets/Scripts/ScriptHeadCommentTest.cs"
string fileContent = File.ReadAllText(fileFullPath); // "using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\npublic class ScriptHeadCommentTest : MonoBehaviour {\r\n\r\n\t// Use this for initialization\r\n\tvoid Start () {\r\n\t\t\r\n\t}\r\n\t\r\n\t// Update is called once per frame\r\n\tvoid Update () {\r\n\t\t\r\n\t}\r\n}\r\n"
string commentContent = "/*\n *FileName: #FILENAME#\n *Author: #AUTHOR#\n *Date: #DATE#\n *UnityVersion: #UNITYVERSION#\n */\n"; // 按照自己的设计添加需要自动生成的信息,调整好间距
commentContent = commentContent.Replace("#FILENAME#", Path.GetFileName(fileFullPath));
commentContent = commentContent.Replace("#AUTHOR#", Environment.UserName);
//commentContent = commentContent.Replace("#DATE#", DateTime.Now.ToString("yyyy/mm/dd hh:mm:ss")); mm表示分钟,hh表示12进制的小时
commentContent = commentContent.Replace("#DATE#", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
commentContent = commentContent.Replace("#UNITYVERSION#", Application.unityVersion);
//fileContent.Insert(0, commentContent); 这里容易出错,不改变原值
fileContent = fileContent.Insert(0, commentContent); // "/*\n *FileName: ScriptHeadCommentTest.cs\n *Author: tangmingzhe\n *Date: 2020/11/12 20:00:16\n *UnityVersion: 2018.2.0f2\n */\nusing System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\npublic class ScriptHeadCommentTest : MonoBehaviour {\r\n\r\n\t// Use this for initialization\r\n\tvoid Start () {\r\n\t\t\r\n\t}\r\n\t\r\n\t// Update is called once per frame\r\n\tvoid Update () {\r\n\t\t\r\n\t}\r\n}\r\n"
File.WriteAllText(fileFullPath, fileContent);
}
}
测试
新建测试脚本ScriptHeadCommentTest
,等待编译完成
/*
*FileName: ScriptHeadCommentTest.cs
*Author: ---------
*Date: 2020/11/12 20:00:16
*UnityVersion: 2018.2.0f2
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptHeadCommentTest : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
最终代码显示如上,名字我特殊处理了,这样处理的好处是只要提交了ScriptHeadComment
这个脚本,团队中的所有人在新建脚本时都会自动生成这些信息,方便记录与追责
如果想自定义脚本模板请移步我这篇文章自定义脚本模板