我們使用 Visual Studio + Unity 開(kāi)發(fā)游戲的時(shí)候 Unity為我們提供了MonoBehaviour 腳本的模板,在 Project 下的 Assets 面板下點(diǎn)擊右鍵,在彈出的菜單中點(diǎn) Create -> C# Script就可以創(chuàng)建MonoBehaviour 腳本。
但是創(chuàng)建出來(lái)的腳本文件不是 utf-8 格式的,導(dǎo)致我們寫(xiě)的注釋經(jīng)常亂碼,而且代碼也沒(méi)有格式化,需要我們手動(dòng)格式化,非常麻煩。所以我們可以編寫(xiě) Editor 腳本來(lái)實(shí)現(xiàn)從我們自定義的模板中創(chuàng)建代碼。除了有以上兩點(diǎn)好處,還可以添加命名空間,或者你想自定義的任何內(nèi)容,非常方便,可以大大減少一些重復(fù)而枯燥的工作。
首先創(chuàng)建模板腳本放在指定的目錄下然后創(chuàng)腳本 CreateClass.cs 放在Editor 目錄下。
CreateClass.cs 腳本內(nèi)容如下:
//---------------------------------------------------------------------
// XRLGame
// Copyright ? 2019-2022 XRLGame Co., Ltd. All rights reserved.
// Author: JerryYang
// Time: 2022-10-21 14:28:41
// Feedback: yang2686022430@163.com
//---------------------------------------------------------------------
using UnityEngine;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
using System.IO;
using System.Text;
namespace XRL
{
/// <summary>
/// 從模板創(chuàng)建文件,有如下好處:
/// 1. 編碼格式統(tǒng)一為UTF-8帶簽名
/// 2. 代碼統(tǒng)一格式化
/// 3. 添加命名空間
/// </summary>
public class CreateClass : MonoBehaviour
{
[MenuItem("Assets/CreateClass/MonoBehaviour")]
public static void CreateMonoBehaviour()
{
string templatePath = "Assets/XRLGame/Editor/Templates/csharp/MonoBehaviour.txt";
Create(templatePath);
}
[MenuItem("Assets/CreateClass/XRLMonoBehaviour")]
public static void CreateXRLMonoBehaviour()
{
string templatePath = "Assets/XRLGame/Editor/Templates/csharp/XRLMonoBehaviour.txt";
Create(templatePath);
}
private static void Create(string path)
{
if (IsSelectedFolder())
{
string folderPath = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);
// 創(chuàng)建代碼文件的過(guò)程中,在重命名時(shí)調(diào)用該方法
Texture2D icon = EditorGUIUtility.IconContent("d_cs Script Icon").image as Texture2D;
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
ScriptableObject.CreateInstance<CreateScriptAsset>(),
folderPath + "/CreateScript.cs",
icon,
path);
}
}
private static bool IsSelectedFolder()
{
return Path.GetExtension(AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0])) == string.Empty;
}
}
public class CreateScriptAsset : EndNameEditAction
{
// 重命名新創(chuàng)建的Script時(shí),調(diào)用它
public override void Action(int instanceId, string pathName, string resourceFile)
{
// 基于模板文件生成具體文件時(shí),需替換文件中的部分內(nèi)容:如類名替換為文件名
UnityEngine.Object obj = CreateTemplateScriptAsset(pathName, resourceFile);
ProjectWindowUtil.ShowCreatedAsset(obj);
}
private static UnityEngine.Object CreateTemplateScriptAsset(string newScriptPath, string templatePath)
{
// 讀入模板內(nèi)容
StreamReader sr = new StreamReader(templatePath);
string content = sr.ReadToEnd();
sr.Close();
// 修改內(nèi)容中的部分內(nèi)容
content = content.Replace("#ScriptName#", Path.GetFileNameWithoutExtension(newScriptPath));
content = content.Replace("#Author#", "JerryYang");
content = content.Replace("#CreateTime#", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
content = content.Replace("#Copyright#", "Copyright ? 2019-2022 XRLGame Co., Ltd. All rights reserved.");
content = content.Replace("#Feedback#", "yang2686022430@163.com.");
// 將修改后的內(nèi)容寫(xiě)入新創(chuàng)建的文件
StreamWriter sw = new StreamWriter(Path.GetFullPath(newScriptPath), false, new UTF8Encoding(true, false));
sw.Write(content);
sw.Close();
// 導(dǎo)入資源
AssetDatabase.ImportAsset(newScriptPath);
return AssetDatabase.LoadAssetAtPath(newScriptPath, typeof(UnityEngine.Object));
}
}
}