這是轉(zhuǎn)自另一篇文章 EditorWindow Custom Context Menu
Unity中的窗口都可以通過右鍵窗口的tab 或者 點(diǎn)擊窗口右上角的一個(gè)菜單按鈕 來顯示窗口菜單項(xiàng)
窗口都有默認(rèn)的菜單項(xiàng)鸡号。合武。可以通過實(shí)現(xiàn) IHasCustomMenu 接口中的 AddItemsToMenu 函數(shù) 來添加自定義的菜單項(xiàng)
代碼:
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);
}
}