文章

顯示包含「Unity Editor」標籤的文章

HideFlags在Unity中的应用

HideFlags的作用(来自UnityEngine官方注释): public enum HideFlags { // // Summary: // A normal, visible object. This is the default. None = 0x0, // // Summary: // The object will not appear in the hierarchy. HideInHierarchy = 0x1, // // Summary: // It is not possible to view it in the inspector. HideInInspector = 0x2, // // Summary: // The object will not be saved to the scene in the editor. DontSaveInEditor = 0x4, // // Summary: // The object is not be editable in the inspector. NotEditable = 0x8, // // Summary: // The object will not be saved when building a player. DontSaveInBuild = 0x10, // // Summary: // The object will not be unloaded by Resources.UnloadUnusedAssets. DontUnloadUnusedAsset = 0x20, // // Summary: // ...

【Unity編輯器拓展 】拓展編輯器菜單

圖片
在日常開發中我們會經常用到Unity的右鍵菜單,這些菜單都是支持自定義的,我們可以透過編寫自己的 Editor腳本 來向其中添加一些自己的選項。 Unity版本:2020.3.12f1 Project視圖右鍵菜單 Hierarchy視圖右鍵菜單 1. 通過Editor腳本拓展Project視圖右鍵菜單 準備工作: 在Project視圖中創建一個名爲 “Editor” 的文件夾作爲Editor腳本的存放點,還可創建一個名爲 “Scripts” 的文件夾並將“Editor”文件夾移入其中以方便整理。 創建腳本: 在Editor文件夾中創建一個腳本,名稱自定,然後打開。 移除類名後的MonoBehaviour繼承,命名空間引用加入using UnityEngine; 和using UnityEditor; 腳本準備工作 添加如下內容。 創建菜單項 其中的static void的名稱可自定爲其他名稱,[MenuItem]方法用於將這個 static void MenuTool1() 方法添加到右鍵菜單中,第一個參數寫入菜單項的路徑“Assets/Expand Tools/Tool1”,第二個參數( isValidateFunction )填入false如果不需要的話,第三個參數爲該菜單項在菜單中的優先級,數值越小越靠前。 菜單項示例 當我們點擊圖中的 "Tool1" 選項便會調用 static void MenuTool1()  方法 還可以使用相同方法拓展Create菜單 拓展Create菜單 效果預覽 2. 通過Editor腳本拓展Hierarchy視圖右鍵菜單 方法與Project視圖相同,將路徑中的“Assets”改爲“GameObject”即可 拓展Hierarchy視圖右鍵菜單 效果預覽 點擊後會在場景中創建一個球體 參考資料 【Unity官方 文檔 】MenuItem