修改Unity创建的C#脚本模板
打开Unity安装目录中的模板配置文件(以我安装的Unity2019.4.14f1c1版本为例)
位置:D:\Program Files\Unity2019.4.14f1c1\Editor\Data\Resources\ScriptTemplates
修改配置文件
分析模板文件名的特点(以81-C# Script-NewBehaviourScript.cs.txt为例):
-之前的C# Script表示在Unity中点击Create时右键菜单显示的文字
-之后的NewBehaviourScript.cs表示执行了创建命令后生的文件的文件名。
修改继承自MonoBehaviour的脚本模板
修改 81-C# Script-NewBehaviourScript.cs.txt
原始代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class #SCRIPTNAME# : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
#NOTRIM#
}
// Update is called once per frame
void Update()
{
#NOTRIM#
}
}
修改后的代码:
增加版权信息、脚本创建时间、作者信息、命名空间,为了代码的一致性,将生命周期函数的访问修饰符改为私有,去掉注释和Update方法**(因为正常在实际开发中的大部分脚本都不会实现Update方法,如果没有实现Update方法确保留了空的Update方法,由于Mono是通过反射的方式调用各个生命周期函数,会造成一定的性能消耗。)**
#region Copyright
// **********************************************************************
// Copyright (C) #COPYRIGHTYEAR# #COMPANYNAME#
//
// Script Name : #SCRIPTNAME#.cs
// Author Name : #AuthorName#
// Create Time : #CreateTime#
// Description :
// **********************************************************************
#endregion
using UnityEngine;
namespace ns
{
public class #SCRIPTNAME# : MonoBehaviour
{
private void Start()
{
#NOTRIM#
}
}
}
在Editor目录下增加脚本用于创建C#脚本时填充Copyright信息:
#region
// **********************************************************************
// Copyright (C) 2021 CompanyName
//
// Script Name : Copyright.cs
// Author Name : MyName
// Create Time : 2021/05/21 13:34:47
// Description : 为创建的C#脚本添加CopyRight信息
// **********************************************************************
#endregion
using System.IO;
using UnityEditor;
namespace Framework.Editor
{
public class Copyright : UnityEditor.AssetModificationProcessor
{
private const string CompanyName = "CompanyName";
private const string AuthorName = "MyName";
private const string DateFormat = "yyy/MM/dd HH:mm:ss";
public static void OnWillCreateAsset(string path)
{
path = path.Replace(".meta", "");
if(path.EndsWith(".cs"))
{
string _fileText = File.ReadAllText(path);
_fileText = _fileText.Replace("#COMPANYNAME#", CompanyName).Replace("#COPYRIGHTYEAR#", System.DateTime.Now.Year.ToString())
.Replace("#AuthorName#", AuthorName).Replace("#CreateTime#", System.DateTime.Now.ToString(DateFormat));
File.WriteAllText(path, _fileText);
AssetDatabase.Refresh();
}
}
}
}
创建Mono的C#脚本效果:
#region Copyright
// **********************************************************************
// Copyright (C) 2021 CompanyName
//
// Script Name : TestScript.cs
// Author Name : MyName
// Create Time : 2021/05/21 13:43:55
// Description :
// **********************************************************************
#endregion
using UnityEngine;
namespace ns
{
public class TestScript : MonoBehaviour
{
private void Start()
{
}
}
}
增加编辑器创建普通C#类文件模板
在D:\Program Files\Unity2019.4.14f1c1\Editor\Data\Resources\ScriptTemplates目录下增加一个txt文件:82-C# Class-NewClass.cs.txt,内容如下:
#region Copyright
// **********************************************************************
// Copyright (C) #COPYRIGHTYEAR# #COMPANYNAME#
//
// Script Name : #SCRIPTNAME#.cs
// Author Name : #AuthorName#
// Create Time : #CreateTime#
// Description :
// **********************************************************************
#endregion
namespace ns
{
public class #SCRIPTNAME#
{
}
}