using UnityEditor; using UnityEngine; //非必需,經常使用到
[MenuItem("Assets/My Test")] private static void Test() { Debug.Log("Hello World!") }
語法說明html
[MenuItem("path $t", Is, Priority)] private static void Test1() {} [MenuItem("path _t", Is, Priority)] private static void Test2() {}
%
表示 ctrl
#
表示 shift
&
表示 alt
下面表示快捷鍵爲"ctrl+h" 的實例。dom
[MenuItem("Assets/My Test %h")] private static void Test() { Debug.Log("Hello World!") }
https://docs.unity3d.com/ScriptReference/Selection.html編輯器
Selection.Objects
能夠得到選中的物品。函數
string[] guids = Selection.assetGUIDs; foreach ( var id in guids ) { string path = AssetDatabase.GUIDToAssetPath(id); if(Directory.Exists(path)) { Debug.Log(string.format("Directory {0} exist", path)) } }
[MenuItem("CONTEXT/Rigidbody/wyg_test")] private static TestRightButton() { Debug.Log("右鍵測試") }
編輯器的彈窗類須要繼承EditorWindow
。測試
使用方法與GUI的使用方法基本一致,有Init,Awake,OnGUI等函數。ui
class TestWindow : EditorWindow { public void Init() { } publick void OnGUI() { GUILayout.Label("Test Window"); if (GUILayout.Button("Button")) { Debug.Log("On Button") } } }
如下效果都是組件的顯示,因此是屬於using UnityEngine;
的。插件
能夠將數值的展現效果變成滑動條效果。3d
[Range(1, 12)] public int month;
控制佔用多行顯示。code
[Multiline(5)] public string msg1; [TextArea(3,5)] public string msg2; // 最少顯示三行,最多顯示五行
添加右鍵小菜單,添加一些小功能。orm
[ContextMenuItem("Random", "RandomNumber")] [ContextMenuItem("Reset", "ResetNumber")] public int number; void RandomNumber() { number = Random.Range(0, 100); } void ResetNumber() { number = 0; }
還有一個ContextMenu
的特性,用法相似。
public string Name; [ContextMenu("Reset Name")] private void ResetName() { Name = ""; }
設置顏色選擇器。
public Color color1; [ColorUsage(false)] public Color color2; // 沒有alpha [ColorUsage(true, true, 0, 8, 0.125f, 3)] public Color color3; // HDR