EditorWindow Custom Context Menu

這是轉(zhuǎn)自另一篇文章 EditorWindow Custom Context Menu

Unity中的窗口都可以通過右鍵窗口的tab 或者 點擊窗口右上角的一個菜單按鈕 來顯示窗口菜單項

右鍵窗口tab
窗口右上角菜單按鈕

窗口都有默認(rèn)的菜單項。??梢酝ㄟ^實現(xiàn) IHasCustomMenu 接口中的 AddItemsToMenu 函數(shù) 來添加自定義的菜單項

代碼:

using UnityEditor;
using UnityEngine;

public class CustomMenuEditorWindow : EditorWindow, IHasCustomMenu
{
    [MenuItem("Window/Custom Menu Window")]
    public static void OpenCustomMenuWindow()
    {
        EditorWindow window = EditorWindow.GetWindow<CustomMenuEditorWindow>();
    }

    private GUIContent m_MenuItem1 = new GUIContent("Menu Item 1");
    private GUIContent m_MenuItem2 = new GUIContent("Menu Item 2");

    private bool m_Item2On = false;


    void Awake()
    {
        titleContent = new GUIContent("Custom Menu");
    }

    //Implement IHasCustomMenu.AddItemsToMenu
    public void AddItemsToMenu(GenericMenu menu)
    {
        menu.AddItem(m_MenuItem1, false, MenuItem1Selected);
        menu.AddItem(m_MenuItem2, m_Item2On, MenuItem2Selected);

        //NOTE: do not show the menu after adding items,
        //      Unity will do that after adding the default
        //      items: maximize, close tab, add tab >
    }

    private void MenuItem1Selected()
    {
        Debug.Log("Menu Item 1 selected");
    }

    private void MenuItem2Selected()
    {
        m_Item2On = !m_Item2On;

        Debug.Log("Menu Item 2 is " + m_Item2On);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容