unity編輯器擴展學習

擴展編輯器實際上就是在unity菜單欄中添加一些按鈕,能夠一鍵執行一些重複性的工做。編輯器

一.添加按鈕spa

1.簡單使用MenuItem特性3d

using UnityEngine;
using UnityEditor;

public class Tools
{
    [MenuItem("Tools/test")]
    static void Test()
    {
        Debug.Log("test");
    }
}

 

 2.路徑設置日誌

在方法上添加MenuItem特性,在MenuItem的括號中傳入路徑字符串參數,路徑中使用/進行目錄分級。第3個參數priority爲選項在目錄中的顯示順序,這個值默認是1000。
using UnityEngine;
using UnityEditor;

public class Tools
{
    //MenuItem的第3個參數priority爲當前選項在菜單欄中的顯示順序,數字越大,顯示在越下面
    //當相鄰兩個選項的priority值相差大於等於11時,系統將在這兩個選項之間添加一條橫線進行分組
    [MenuItem("Tools/test1",false,2)]
    static void Test1()
    {
        Debug.Log("test1");
    }

    [MenuItem("Tools/test2",false,1)]
    static void Test2()
    {
        Debug.Log("test2");
    }

    [MenuItem("Tools/test3",false,13)]
    static void Test3()
    {
        Debug.Log("test3");
    }
}

3.Hierarchy和Project窗口的右鍵菜單code

Hierarchy窗口中右鍵的菜單其實是一些複製粘貼等按鈕和GameObject目錄下第一類的按鈕的組合,所以將按鈕添加到GameObject菜單下第一類按鈕中便可在Hierachy窗口中右鍵出這個按鈕。對象

Project窗口的右鍵菜單實際上就是Assets目錄,所以將按鈕添加到Assets目錄下便可在Project窗口中右鍵出這個按鈕。blog

4.在組件的右鍵菜單中添加按鈕繼承

using UnityEditor;
using UnityEngine;

public class PlayerEditor
{
    //在組件上的右鍵菜單中添加按鈕路徑參數爲「CONTEXT/須要添加按鈕的組件名稱/按鈕目錄和名稱」
    [MenuItem("CONTEXT/PlayerHealth/InitHealthAndSpeed")]
    static void InitHealthAndSpeed(MenuCommand command)
    {
        //在按下按鈕後,系統自動傳遞參數MenuCommand,對象的context屬性的內容就是當前組件
        CompleteProject.PlayerHealth health = command.context as CompleteProject.PlayerHealth;
        //獲取當前組件後,能夠進行修改組件的變量等操做
        health.startingHealth = 10000;
        health.flashSpeed = 15;
    }
}

5.Selection獲取選擇的遊戲物體遊戲

using UnityEditor;
using UnityEngine;

public class PlayerEditor
{
    [MenuItem("Test/showInfo")]
    static void InitHealthAndSpeed()
    {
        Debug.Log(Selection.objects.Length);
    }
}

 

 6.按鈕快捷鍵ip

using UnityEditor;
using UnityEngine;

public class PlayerEditor
{
    //在路徑名稱後空格再指定快捷鍵,這裏指定快捷鍵是T
    //一些快捷鍵例子:_t ==> T
    //              %t ==> Ctrl+T
    //              #t ==> Shift+T
    //              &t ==> Alt+T
    [MenuItem("Test/showInfo _t")]
    static void InitHealthAndSpeed()
    {
        Debug.Log(Selection.objects.Length);
    }
}

7.按鈕的驗證方法

using UnityEditor;
using UnityEngine;

public class PlayerEditor
{
    //按鈕的驗證方法和按鈕方法的菜單路徑一致,返回值爲bool值,將MenuItem的第二個參數置爲true表明這是一個按鈕的驗證方法
    //這個方法判斷選中的遊戲物體數,若是選中了任意遊戲物體才能點擊按鈕;未選中游戲物體按鈕將變爲不可點擊狀態
    [MenuItem("Test/showInfo _t",true,11)]
    static bool InitHealthAndSpeedValidate()
    {
        if (Selection.objects.Length > 0)
            return true;
        else
            return false;
    }

    //日誌輸出選中的遊戲物體數目
    [MenuItem("Test/showInfo _t",false,11)]
    static void InitHealthAndSpeed()
    {
        Debug.Log(Selection.objects.Length);
    }
}

8.爲組件添加按鈕的另外一種方式

使用ContextMenu特性爲組件添加按鈕,注意這個特性添加在一個具體的方法上方,表明按鈕觸發這個方法,而這個方法必須是要添加按鈕的腳本內的方法,能夠理解爲直接在腳本中定義按鈕。

        //爲腳本添加SetColor按鈕,按鈕觸發這個方法改變腳本中flashColour的屬性值
        [ContextMenu("SetColor")]
        void SetColor()
        {
            flashColour = Color.blue;
        }

使用ContextMenuItem特性爲腳本的某個屬性添加按鈕,這個特性天然也就定義在須要添加按鈕的屬性上方,按鈕觸發的方法也定義在腳本中。

        //這個特性表明爲startingHealth屬性添加一個AddHP100按鈕,在屬性上右鍵便可看到這個按鈕,這個按鈕會觸發AddHP這個方法
        [ContextMenuItem("AddHP100","AddHP")]
        public int startingHealth = 100;
        void AddHP()
        {
            startingHealth += 100;
        }

二.添加和使用對話框

1.簡單添加對話框

首先建立一個按鈕用於彈出對話框。

using UnityEditor;
using UnityEngine;

public class PlayerEditor
{
    [MenuItem("Tools/CreateWizard")]
    static void CreateWizard()
    {
        //按下按鈕後彈出對話框,對話框的標題爲「這是對話框」,對話框的內容由ShowDialog這個腳本進行定義
        ScriptableWizard.DisplayWizard<ShowDialog>("這是對話框");
    }
}

而後定義對話框,對話框須要繼承自ScriptableWizard類。

using UnityEditor;

public class ShowDialog : ScriptableWizard
{
    public int changeHealthValue = 100;
}

最後保存後在unity中點擊按鈕就能夠看到對話框了。

 

 注意:推薦將按鈕的代碼寫在對話框的類中,結構會更加清晰,方便管理。

2.DisplayWizard方法的參數

using UnityEditor;

public class ShowDialog : ScriptableWizard
{
    [MenuItem("Tools/CreateWizard")]
    static void CreateWizard()
    {
        //兩個參數都是字符串,第一個參數定義對話框的標題,第二個參數定義右下角確認提交按鈕的名稱,默認是Create
        ScriptableWizard.DisplayWizard<ShowDialog>("這是對話框","Change");
    }

    public int changeHealthValue = 100;

 

 3.在對話框確認提交後觸發OnWizardCreate方法

using UnityEditor;
using UnityEngine;

public class ShowDialog : ScriptableWizard
{
    [MenuItem("Tools/CreateWizard")]
    static void CreateWizard()
    {
        //兩個參數都是字符串,第一個參數定義對話框的標題,第二個參數定義右下角確認提交按鈕的名稱,默認是Create
        ScriptableWizard.DisplayWizard<ShowDialog>("這是對話框","Change");
    }

    public int changeHealthValue = 100;

    //監測對話框確認提交按鈕,對話框確認提交後觸發
    private void OnWizardCreate()
    {
        GameObject[] gos = Selection.gameObjects;
        foreach(GameObject go in gos)
        {
            go.GetComponent<EnemyHealth>().startingHealth += changeHealthValue;
        }
    }
}

4.ScriptableWizard類中的其餘Message

OnWizardUpdate方法:當打開對話框或對話框中的值發生改變時觸發。

OnWizardOtherButton方法:當用戶點擊others按鈕時觸發,這個按鈕能夠經過DisplayWizard方法的第3個參數指定名稱,others按鈕不會關閉對話框。

 

三.使用EditorWindow類建立窗口

using UnityEngine;
using UnityEditor;

public class ShowWindow : EditorWindow
{
    [MenuItem("Window/show mywindow")]
    static void ShowMyWindow()
    {
        ShowWindow window = EditorWindow.GetWindow<ShowWindow>("MyWindow");
        window.Show();
    }

    private void OnGUI()
    {
        GUILayout.Label("個人窗口");
        GUILayout.TextField("");
        GUILayout.Button("建立");
    }
}

相關文章
相關標籤/搜索