在项目开发的过程中,我们需要对一个.cs文件添加一些注释(例如:创建者,创建日期,该类的作用),或者给创建的类增加某个通用的函数。如果我们每创建一个.cs文件都需要添加这些会影响我们的开发效率,会很麻烦。那么我们本着“多一事不如少一事”的原则,这个时候就需要一个模版来解决这个问题。
路径
首先我们要找到脚本模板文件。当然每个人在电脑上安装的目录不一样,所以注意前面目录及unity版本的问题。
其路径有:
Window:
Unity安装目录\Editor\Data\Resources\ScriptTemplates\81-C# Script-NewBehaviourScript.cs.txt
Mac:
Unity.app/Contents/Resources/ScriptTemplates/81-C# Script-NewBehaviourScript.cs.txt
模板文件
打开81-C# Script-NewBehaviourScript.cs.txt文件,我们可以先看一下Unity默认创建.cs文件是它的格式:
public class #SCRIPTNAME# : MonoBehaviour {
// Use this for initialization
void Start () {
#NOTRIM#
}
// Update is called once per frame
void Update () {
#NOTRIM#
}
}
这个模版正是我们每次创建.cs文件时对应的模版,这个时候我们把这个模版改掉之后我们创建的.cs文件默认的文本也会改变。
/****************************************************
* Copyright © 2018-2020 #AUTHORNAME#. All rights reserved.
*------------------------------------------------------------------------
* 文件:#SCRIPTNAME##FILEEXTENSION#
* 作者:Plane
* 邮箱: [email protected]
* 日期:Created by #SMARTDEVELOPERS# on #CREATETIME#
* 项目:#PROJECTNAME#
* 功能:Nothing
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ZM_Code
{
public class #SCRIPTNAME# : MonoBehaviour
{
void Awake()
{
#NOTRIM#
}
void Start ()
{
#NOTRIM#
}
void Update ()
{
#NOTRIM#
}
}
}
以上便是我所修改的模板文本。
这个时候我们去Unity里面创建一个.cs文件,发现它的创建内容确实变了。
/****************************************************
* Copyright © 2018-2020 ZM. All rights reserved.
*------------------------------------------------------------------------
* 文件:App.cs
* 作者:zm
* 邮箱: [email protected]
* 日期:Created by IVLab on 12/25/2019 14:41:49
* 项目:VirtualTrainingPlatformV4.0
* 功能:Nothing
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ZM_Code
{
public class App : MonoBehaviour
{
void Awake()
{
}
void Start ()
{
}
void Update ()
{
}
}
}
但是这个时候又有一个问题出现了,我们创建出来的文本的注释不会随着时间的推移进行改动,或者说如何在创建一个.cs文件的时候就将需要修改的地方进行修改。
我们自己新建的脚本模板文件里面有自己添加的几个预定义的key。然后只要在新建脚本的时候替换这几个key为对应的内容,便能够完成上述功能。
我们就需要在项目的Editor文件夹下创建以下脚本(.cs):
using System;
using System.IO;
using UnityEditor;
namespace ZM_Code
{
public class ScriptsInfoEditor : UnityEditor.AssetModificationProcessor
{
public const string authorName = "tackor(修改为你自己的名称即可)";
private static void OnWillCreateAsset(string path)
{
path = path.Replace(".meta", "");
if (path.EndsWith(".cs"))
{
string str = File.ReadAllText(path);
str = str.Replace("#AUTHORNAME#", authorName);
str = str.Replace("#FILEEXTENSION#", path.Substring(path.LastIndexOf(".")));
str = str.Replace("Plane", Environment.UserName);
str = str.Replace("#SMARTDEVELOPERS#", PlayerSettings.companyName);
str = str.Replace("#CREATETIME#", string.Concat(DateTime.Now.ToString("d"), " ", DateTime.Now.Hour, ":", DateTime.Now.Minute, ":", DateTime.Now.Second));
str = str.Replace("#PROJECTNAME#", PlayerSettings.productName);
File.WriteAllText(path, str);
}
}
}
}
注意:
1.在创建新脚本之前,需要先将脚本ScriptsInfoEditor.cs拖入到工程目录下的Editor(如果没有就自己创建一 个命名为Editor的文件夹)目录中去。
2.设置PlayerSettings的属性,点击Edit/Project Settings/Player,修改Company Name为所需要的名字。
这个时候我们发现再次新建的.cs脚本文件它的日期变了,而且其他的我们需要修改得其他的一些模版信息,也随着设定出现了改变。