開發unity外掛——一次搞定unity編輯器常用功能

cloudysong發表於2017-01-13

開發unity外掛——一次搞定unity編輯器常用功能

unity

這篇文章主要分享unity中與editor外掛等相關的使用,比較基礎,不過如果都掌握了就可以擴充套件寫一些unity外掛了,平時開發中也會提升工作效率。

editor相關指令碼一定要放在Editor資料夾下,繼承monobehaviour的檔案不要放到Editor資料夾下。

monobehaviour相關的編輯器功能

首先常用的在繼承monobehaviour類中寫public變數可以在inspector中序列化可編輯一般人都知道了,下面是一些可以更有效率更酷的方法。

增強序列化屬性

```csharp 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來組織皮膚的外觀,也可以針對不同的屬性進行是否序列化的選擇。

serialize

序列化類

也可以序列化一個類

csharp [Serializable]//一個可序列化的類 public class SerializableClass { public int x = 0; public Vector2 pos; public Color color; public Sprite sprite; } public SerializableClass serializedObject;//一個可序列化的類的例項

serializedObject

元件皮膚的上下文選單

有時在monobehaviour中寫一些方法可以初始化一些值或者隨機產生某個值這種需求,都可以在選單中觸發,只要簡單的加一行即可。

```csharp [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工具了

contextmenu

inspector相關的編輯器功能

如果要在inspector中加上一些更高階的功能就需要使用editor相關的方法了

這是要使用的TestInspector類程式碼 ```csharp [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事件處理。

裡面還註冊了UnDo,好處是可以通過ctrl+z來進行撤銷操作,這樣才更完美更像一個完善的unity外掛。

程式碼也沒什麼難度,我也做了下簡單的註釋,執行一下看看效果大部分人就都理解了。效果如下圖

inspector

inspector

各種上下文選單

元件選單

之前可以在monobehaviour中加入[ContextMenu("Random value")]來生成對應指令碼元件皮膚的上下文選單,那麼如果要生成一個在transform元件上的選單怎麼辦

csharp [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之類的元件加上一些屬性模板的設定,會很方便吧

TransformMenu

帶勾選的選單

在editor中加個選單item是件很容易的事情,那麼如果這個選單是可以勾選的呢?是不是可以解決一些開關的問題?

```csharp 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上就寫到登錄檔裡了

checkedmenu

project皮膚中的選單

這個選單是加到了Assets下面,那麼在project皮膚中右鍵也可以看到,這種選單可以幹什麼呢,我也沒想好,不過幹些修改assetsimport屬性或者修改一些資源等等還是挺好用的吧

```csharp [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就知道了

AssetsMenu

AssetsMenu

hierarchy皮膚選單

這個選單還是比較實用的,相對來說也不太一樣

csharp [MenuItem("GameObject/Create Other/TestGameObject")]//將選單放到GameObject選單中,可以在hierarchy中看到 static void MenuGameObject() { Debug.Log("TestGameObject"); } 將選單加到GameObject下面,就可以在hierarchy裡右鍵看到了

GameObjectMenu

那麼基於這個選單我們可以做個比較實用的功能,例如右鍵hierarchy中場景的一個GameObject並且對它進行SetActive為true or false的操作,程式碼如下:

```csharp //快捷鍵可以為%=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,還是比較實用的吧,基於此可以做很多實用的東西。效果如下圖

SetActiveMenu

SetActiveMenu

對話方塊

對話方塊比較簡單,就是一些內建的api,具體可以檢視api,支援了例如簡單和複雜對話方塊、開啟儲存檔案對話方塊、進度條等等功能

EditorUtility.DisplayCancelableProgressBar("ok", "done", 0.7f) EditorUtility.ClearProgressBar(); EditorUtility.OpenFilePanel("open", "d:/", ".txt");

progress

Dialog

新視窗

如果要做的事情可能不是與某個GameObject相關,inspector不能滿足要求,那麼可以建立一個新的視窗,建立新的editor視窗需要繼承EditorWindow,程式碼如下

```csharp [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繪製,效果如下:

newwindow

newwindow

後續

如果有更高的需求可能需要更深入的研究一下unity中editor的相關api和文件

unity還提供了可以在scene視窗中做一些操作,例如畫一些輔助線、顯示label、操作handler等,具體可以參考 http://blog.csdn.net/kun1234567/article/details/19421471

結語

如果把這些程式碼執行一遍,改改除錯一下,理解基本流程,那麼已經可以寫一些提高工作效率的unity外掛了

VR開發或者unity相關交流可以郵件madcloudsong@qq.com 轉載請註明原文連結 http://blog.csdn.net/madcloudsong/article/details/54408103

相關文章