Unity自定義建立指令碼模板選單
阿新 • • 發佈:2019-02-20
unity中我們可以在Project面板右鍵或通過Assets選單建立指令碼或是shader,unity為我們提供了C#,javascript指令碼以及四個shader程式碼的模板,當建立這些指令碼時實際上是複製這些模板並改變其類名或是shader名,我們可以在Unity的安裝目錄下找到這些程式碼模板檔案:D:\Program Files\Unity\Editor\Data\Resources\ScriptTemplates。
我們可以自定義自己的檔案模板,然後寫指令碼建立右鍵選單,這樣在Assets選單中右鍵Create時可以建立自己的模板指令碼。
注意將自己的模板檔案放在Assets資料夾下任意位置的Editor資料夾下。這裡建立c#指令碼的模板名為:EventCSClass。
程式碼如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; using UnityEditor.ProjectWindowCallback; using System.Text.RegularExpressions; using System; using System.Text; //這是一個編輯器類,如果想使用它你需要把它放到工程目錄下的Assets/Editor資料夾下。 //編輯器類在UnityEditor名稱空間下。所以當使用C#指令碼時,你需要在指令碼前面加上 //"using UnityEditor"引用 public class CreateLuaAuto { //選單路徑,是否為驗證方法,選單項優先順序 [MenuItem("Assets/Create/U3DMy Lua Script", false, 80)] //方法必須為靜態方法 public static void CreateLua() { //將設定焦點到某檔案並進入重新命名 ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance<CreateLuaScriptAsset>(), GetSelectPathOrFallback() + "/New Lua.lua", null, "Assets/Frame/Editor/LuaClass.lua"); } [MenuItem("Assets/Create/U3DMy c#Script", false, 70)] public static void CreateEventCS() { //引數為傳遞給CreateEventCSScriptAsset類action方法的引數 ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance<CreateEventCSScriptAsset>(), GetSelectPathOrFallback() + "/New Script.cs", null, "Assets/Frame/Editor/EventCSClass.cs"); } //取得要建立檔案的路徑 public static string GetSelectPathOrFallback() { string path = "Assets"; //遍歷選中的資源以獲得路徑 //Selection.GetFiltered是過濾選擇檔案或資料夾下的物體,assets表示只返回選擇物件本身 foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets)) { path = AssetDatabase.GetAssetPath(obj); if (!string.IsNullOrEmpty(path) && File.Exists(path)) { path = Path.GetDirectoryName(path); break; } } return path; } } //要建立模板檔案必須繼承EndNameEditAction,重寫action方法 class CreateEventCSScriptAsset : EndNameEditAction { public override void Action(int instanceId, string pathName, string resourceFile) { //建立資源 UnityEngine.Object obj = CreateScriptAssetFromTemplate(pathName, resourceFile); ProjectWindowUtil.ShowCreatedAsset(obj);//高亮顯示資源 } internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile) { //獲取要建立資源的絕對路徑 string fullPath = Path.GetFullPath(pathName); //讀取本地的模板檔案 StreamReader streamReader = new StreamReader(resourceFile); string text = streamReader.ReadToEnd(); streamReader.Close(); //獲取檔名,不含副檔名 string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName); Debug.Log("text==="+text); //將模板類中的類名替換成你建立的檔名 text = Regex.Replace(text, "EventCSClass", fileNameWithoutExtension); bool encoderShouldEmitUTF8Identifier = true; //引數指定是否提供 Unicode 位元組順序標記 bool throwOnInvalidBytes = false;//是否在檢測到無效的編碼時引發異常 UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes); bool append = false; //寫入檔案 StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding); streamWriter.Write(text); streamWriter.Close(); //重新整理資源管理器 AssetDatabase.ImportAsset(pathName); AssetDatabase.Refresh(); return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object)); } } class CreateLuaScriptAsset : EndNameEditAction { public override void Action(int instanceId, string pathName, string resourceFile) { UnityEngine.Object obj = CreateScriptAssetFromTemplate(pathName, resourceFile); ProjectWindowUtil.ShowCreatedAsset(obj); } internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile) { string fullPath = Path.GetFullPath(pathName); StreamReader streamReader = new StreamReader(resourceFile); string text = streamReader.ReadToEnd(); streamReader.Close(); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName); Debug.Log("text===" + text); text = Regex.Replace(text, "LuaClass", fileNameWithoutExtension); bool encoderShouldEmitUTF8Identifier = true; bool throwOnInvalidBytes = false; UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes); bool append = false; StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding); streamWriter.Write(text); streamWriter.Close(); AssetDatabase.ImportAsset(pathName);//匯入指定路徑下的資源 return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));//返回指定路徑下的所有Object物件 } }
參考部落格:
建立模板:
選單項擴充套件: