Reference blogger: Unity modifies the created script template, Unity script template path_How to change the script template in Unity hub_Mr. Silent Blog-CSDN Blog
First, put the script Editor
in the folder
using UnityEditor;
using UnityEngine;
using System.IO;
public class AddFileHeadComment : UnityEditor.AssetModificationProcessor
{
/// <summary>
/// 此函数在asset被创建完,文件已经生成到磁盘上,但是没有生成.meta文件和import之前被调用
/// </summary>
/// <param name="newFileMeta">newfilemeta 是由创建文件的path加上.meta组成的</param>
public static void OnWillCreateAsset(string newFileMeta)
{
string newFilePath = newFileMeta.Replace(".meta", "");
string fileExt = Path.GetExtension(newFilePath);
if (fileExt != ".cs")
{
return;
}
//注意,Application.datapath会根据使用平台不同而不同
string realPath = Application.dataPath.Replace("Assets", "") + newFilePath;
string scriptContent = File.ReadAllText(realPath);
//这里实现自定义的一些规则
scriptContent = scriptContent.Replace("#SCRIPTNAME#", Path.GetFileName(newFilePath));
scriptContent = scriptContent.Replace("#COMPANY#", PlayerSettings.companyName);
scriptContent = scriptContent.Replace("#AUTHOR#", "Passion");
scriptContent = scriptContent.Replace("#VERSION#", "1.0");
scriptContent = scriptContent.Replace("#UNITYVERSION#", Application.unityVersion);
scriptContent = scriptContent.Replace("#CREATETIME#", System.DateTime.Now.ToString("F"));
File.WriteAllText(realPath, scriptContent);
}
}
Second, find the script template that comes with unity
Find the file name: 81-C# Script-NewBehaviourScript.cs
Use the code area to modify it (you can add some commonly used drinks by yourself)
/**********************************************************************
文件信息
文件名(File Name): #SCRIPTNAME#.cs
作者(Author): TianWenQuan
创建时间(CreateTime): #CREATETIME#
Unity版本(UnityVersion): #UNITYVERSION#
项目:**虚拟仿真实验
**********************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
namespace Twq
{
public class #SCRIPTNAME# : MonoBehaviour
{
void Awake()
{
#NOTRIM#
}
void Update()
{
#NOTRIM#
}
}
}