這篇文章主要分享unity中與editor插件等相關的使用,比較基礎,不過若是都掌握了就能夠擴展寫一些unity插件了,平時開發中也會提高工做效率。windows
editor相關腳本必定要放在Editor文件夾下,繼承monobehaviour的文件不要放到Editor文件夾下。api
首先經常使用的在繼承monobehaviour類中寫public變量能夠在inspector中序列化可編輯通常人都知道了,下面是一些能夠更有效率更酷的方法。dom
public bool isGood = false; [Tooltip("hp")]//鼠標hover的時候顯示一個tooltip public int life = 0; [Range(0f, 1f)]//float slider public float CloudRange = 0.5f; [Range(0, 15)]//int slider public int CloudRangeInt = 1; [Header("OtherAttr")]//能夠將屬性隔離開,造成分組的感受 public float CloudHeader = 1f; [Space(30)]//能夠與上面造成一個空隙 public float CloudSpace = 1f; [HideInInspector]//使屬性在inspector中隱藏,可是仍是可序列化,想賦值能夠經過寫程序賦值序列化 public float CloudHideInInspector = 1f; [NonSerialized]//使public屬性不能序列化 public float CloudNonSerialized = 1f; [SerializeField]//使private屬性能夠被序列化,在面板上顯示而且能夠讀取保存 private bool CloudSerializeField = true;
效果以下圖,對於一些有範圍的數值能夠用range作個slider讓策劃來調節,能夠用header和space來組織面板的外觀,也能夠針對不一樣的屬性進行是否序列化的選擇。編輯器
也能夠序列化一個類ide
[Serializable]//一個可序列化的類 public class SerializableClass { public int x = 0; public Vector2 pos; public Color color; public Sprite sprite; } public SerializableClass serializedObject;//一個可序列化的類的實例
有時在monobehaviour中寫一些方法能夠初始化一些值或者隨機產生某個值這種需求,均可以在菜單中觸發,只要簡單的加一行便可。函數
[ContextMenu("Init")]//能夠在組件的右鍵菜單及設置(那個小齒輪按鈕)菜單看到, void Init() { isGood = false; life = 0; } [ContextMenu("Random value")] void RandomValue() { Debug.Log("TestContextMenu " + gameObject.name); isGood = true; life = UnityEngine.Random.Range(1, 100); }
效果以下圖,點擊init就會賦一個初始的值,點擊randomvalue能夠隨機產生一個life的值,這就是最簡單的editor工具了工具
若是要在inspector中加上一些更高級的功能就須要使用editor相關的方法了ui
這是要使用的TestInspector類代碼spa
[CustomEditor(typeof(TestInspector))] public class CloudTools : Editor { #region inspector TestInspector script;//所對應的腳本對象 GameObject rootObject;//腳本的GameObject SerializedObject seriObject;//所對應的序列化對象 SerializedProperty headColor;//一個[SerializeField][HideInInspector]且private的序列化的屬性 private static bool toggle = true;//toggle按鈕的狀態 //初始化 public void OnEnable() { seriObject = base.serializedObject; headColor = seriObject.FindProperty("headColor"); var tscript = (TestInspector)(base.serializedObject.targetObject); if (tscript != null) { script = tscript; rootObject = script.gameObject; }else { Console.Error.WriteLine("tscript is null"); } } //清理 public void OnDisable() { var tscript = (TestInspector)(base.serializedObject.targetObject); if (tscript == null) { // 這種狀況是腳本對象被移除了; Debug.Log("tscript == null"); } else { // 這種狀況是編譯腳本致使的重刷; Debug.Log("tscript != null"); } seriObject = null; script = null; rootObject = null; } public override void OnInspectorGUI() { base.OnInspectorGUI(); seriObject.Update(); //將target轉化爲腳本對象 script = target as TestInspector; //random按鈕 if (GUILayout.Button("RandomNum")) { //註冊undo,能夠在edit菜單裏看到undo,也能夠經過ctrl+z來回退 Undo.RecordObject(script, "revert random num"); script.RandomNum(script.num); } //save scene和toggle這組按鈕 GUILayout.BeginHorizontal(); { if (GUILayout.Button("SaveScene")) { EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo(); } if(GUILayout.Button(toggle ? "untoggle" : "toggle")) { toggle = !toggle; } } GUILayout.EndHorizontal(); script.isAlive = EditorGUILayout.BeginToggleGroup("isAlive", script.isAlive); if (script.isAlive)//若是isAlive不勾選則不顯示life { script.life = EditorGUILayout.Slider("life", script.life, 0, 100f); } EditorGUILayout.EndToggleGroup(); //能夠顯示TestInspector中序列化可是不在inspector中顯示的屬性 EditorGUILayout.PropertyField(headColor); seriObject.ApplyModifiedProperties(); //展現普通訊息 EditorGUILayout.LabelField("life " + script.life, GUILayout.Width(200)); Repaint(); } #endregion }
其中須要用OnEnable和OnDisable來作初始化和清理工做,OnInspectorGUI方法能夠類比monobehaviour中的OnGUI,作ui渲染和ui事件處理。.net
裏面還註冊了UnDo,好處是能夠經過ctrl+z來進行撤銷操做,這樣才更完美更像一個完善的unity插件。
代碼也沒什麼難度,我也作了下簡單的註釋,執行一下看看效果大部分人就都理解了。效果以下圖
以前能夠在monobehaviour中加入[ContextMenu("Random value")]
來生成對應腳本組件面板的上下文菜單,那麼若是要生成一個在transform組件上的菜單怎麼辦
[MenuItem("CONTEXT/Transform/RandomPosition")] static void ContextMenu_TransformRandomPosition()//隨機改變transform組件的position { Debug.Log("ContextMenu_Transform"); Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable); foreach (Transform transform in transforms) { transform.localPosition = new Vector3(UnityEngine.Random.Range(-10, 10), UnityEngine.Random.Range(-10, 10), UnityEngine.Random.Range(-10, 10)); Debug.Log(transform.localPosition); } }
效果以下圖,若是策劃或者美術須要對transform的position幹這種隨機的事,是否是就能夠這麼搞了?或者對collider、rigibody之類的組件加上一些屬性模板的設置,會很方便吧
在editor中加個菜單item是件很容易的事情,那麼若是這個菜單是能夠勾選的呢?是否是能夠解決一些開關的問題?
const string Menu_Checked = "Cloud/MenuChecked";//checked menu的名字 const string Key_MenuChecked = "MenuChecked";//checked menu狀態存儲的key [MenuItem(Menu_Checked)] static void MenuChecked() { bool flag = Menu.GetChecked(Menu_Checked); if (flag) { Debug.Log("Key_MenuChecked to 0"); PlayerPrefs.SetInt(Key_MenuChecked, 0);//經過存儲0和1來判斷是否check menu } else { Debug.Log("Key_MenuChecked to 1"); PlayerPrefs.SetInt(Key_MenuChecked, 1); } Menu.SetChecked(Menu_Checked, !flag); } [MenuItem(Menu_Checked, true)]//判斷menu是否check的函數 public static bool IsMenuChecked() { Menu.SetChecked(Menu_Checked, PlayerPrefs.GetInt(Key_MenuChecked, 0) == 1); return true; }
效果以下圖,其中須要一個菜單的valid函數來判斷菜單是否在勾選狀態,這裏用了playprefs,在windows上就寫到註冊表裏了
這個菜單是加到了Assets下面,那麼在project面板中右鍵也能夠看到,這種菜單能夠幹什麼呢,我也沒想好,不過幹些修改assetsimport屬性或者修改一些資源等等仍是挺好用的吧
[MenuItem("Assets/TestAssets")] static void MenuAssets() { if(Selection.activeObject == null) { Debug.Log("TestAssets choose null"); } else { Debug.Log("TestAssets name = " + Selection.activeObject.name); } }
通常這種菜單均可以經過Selection.activeObject/activeGameObject
等等來獲取選中對象,固然也能夠獲取多選的多個對象,這個看下api就知道了
這個菜單仍是比較實用的,相對來講也不太同樣
[MenuItem("GameObject/Create Other/TestGameObject")]//將菜單放到GameObject菜單中,能夠在hierarchy中看到 static void MenuGameObject() { Debug.Log("TestGameObject"); }
將菜單加到GameObject下面,就能夠在hierarchy裏右鍵看到了
那麼基於這個菜單咱們能夠作個比較實用的功能,例如右鍵hierarchy中場景的一個GameObject而且對它進行SetActive爲true or false的操做,代碼以下:
//快捷鍵能夠爲%=ctrl/cmd #=shift &=alt LEFT/RIGHT/UP/DOWN F1-F12,HOME END PGUP PGDN _a~_z [MenuItem("GameObject/SetActive _a", false, 11)] //11及之後能夠在Camera以後顯示 static void MenuGameObjectSetActive()//經過按a鍵來設置所選擇GameObject的active狀態 { Debug.Log("MenuGameObjectSetActive"); if(Selection.activeGameObject != null) { Undo.RecordObject(Selection.activeGameObject, "SetActive" + Selection.activeGameObject.activeSelf + " " + Selection.activeGameObject.name); Selection.activeGameObject.SetActive(!Selection.activeGameObject.activeSelf);//就算鎖定了inpector,也是處理當前選中的 } Debug.Log(Selection.activeObject.name); } [MenuItem("GameObject/SetActive", true, 11)] static bool CheckIsGameObject()//判斷是否顯示該菜單的校驗方法,若是沒選擇GameObject爲灰 { UnityEngine.Object selectedObject = Selection.activeObject; if(selectedObject != null && selectedObject.GetType() == typeof(GameObject)) { Debug.Log(selectedObject.name); return true; } return false; }
其中作校驗的方法是爲了在不選中GameObject的時候可以將菜單灰掉。另外這種菜單能夠綁定一個快捷鍵,這個例子是綁定了a鍵,菜單中也能夠看出來。
最終效果就成了:我選中一個GameObject,只要按下a鍵就能夠SetActive(false),再按下變成true,仍是比較實用的吧,基於此能夠作不少實用的東西。效果以下圖
對話框比較簡單,就是一些內置的api,具體能夠查看api,支持了例如簡單和複雜對話框、打開保存文件對話框、進度條等等功能
EditorUtility.DisplayCancelableProgressBar("ok", "done", 0.7f)
EditorUtility.ClearProgressBar();
EditorUtility.OpenFilePanel("open", "d:/", ".txt");
若是要作的事情可能不是與某個GameObject相關,inspector不能知足要求,那麼能夠建立一個新的窗口,建立新的editor窗口須要繼承EditorWindow,代碼以下
[CustomEditor(typeof(CloudWindow))] public class CloudWindow : EditorWindow { #region 對話框 //經過MenuItem按鈕來建立這樣的一個對話框 [MenuItem("Cloud/ShowEditorTestPanel")] public static void ConfigDialog() { EditorWindow.GetWindow(typeof(CloudWindow)); } public UnityEngine.Object go = null; string goName= "default"; float life = 100f; bool isAlive = true; bool toggleEnabled; void OnGUI() { //Label GUILayout.Label("Label Test", EditorStyles.boldLabel); //經過EditorGUILayout.ObjectField能夠接受Object類型的參數進行相關操做 go = EditorGUILayout.ObjectField(go, typeof(UnityEngine.Object), true); //Button if (GUILayout.Button("Button Test")) { if (go == null) { Debug.Log("go == null"); } else { Debug.Log(go.name); } } goName = EditorGUILayout.TextField("textfield", goName); toggleEnabled = EditorGUILayout.BeginToggleGroup("optional settings", toggleEnabled); if (toggleEnabled) { isAlive = EditorGUILayout.Toggle("isalive", isAlive); life = EditorGUILayout.Slider("life", life, 0, 100); } EditorGUILayout.EndToggleGroup(); } #endregion }
好像代碼也不復雜,也沒什麼難度就是常見的ui繪製,效果以下:
若是有更高的需求可能須要更深刻的研究一下unity中editor的相關api和文檔
unity還提供了能夠在scene窗口中作一些操做,例如畫一些輔助線、顯示label、操做handler等,具體能夠參考
http://blog.csdn.net/kun12345...
若是把這些代碼執行一遍,改改調試一下,理解基本流程,那麼已經能夠寫一些提升工做效率的unity插件了
VR開發或者unity相關交流能夠郵件madcloudsong@qq .com
轉載請註明原文連接
http://blog.csdn.net/madcloud...