这是转自另一篇文章 EditorWindow Custom Context Menu
Unity中的窗口都可以通过右键窗口的tab 或者 点击窗口右上角的一个菜单按钮 来显示窗口菜单项
窗口都有默认的菜单项。。可以通过实现 IHasCustomMenu 接口中的 AddItemsToMenu 函数 来添加自定义的菜单项
代码:
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);
}
}