Context-clicking an EditorWindow tab shows the context menu for that window. The standard context menu has the items: Maximize, Close Tab, and Add Tab. Some of the built-in windows have extra items in their context menus, such as the Inspector which also has: Normal, Debug, and Lock.
I’m going to show you how to add your own custom menu items to your window’s context menu. It’s really simple. You just have to implement the IHasCustomMenu interface like so.
using UnityEditor; using UnityEngine; public class CustomMenuEditorWindow : EditorWindow, IHasCustomMenu { private GUIContent m_MenuItem1 = new GUIContent("Menu Item 1"); private GUIContent m_MenuItem2 = new GUIContent("Menu Item 2"); private bool m_Item2On = false; //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); } [MenuItem("Tools/Custom Menu Window")] public static void Init_ToolsMenu() { EditorWindow window = EditorWindow.GetWindow<CustomMenuEditorWindow>(); window.title = "Custom Menu"; window.Show(); } }
The method of note is AddItemsToMenu(GenericMenu menu). Inside the method body, add the menu items you need in order from top to bottom. If you need to, take a moment to read up on GenericMenu.
Here are the results of the code above.
Context-clicking the window’s tab will show the menu as will clicking the little icon on the right over there. If an item should be checked, just pass a value of true to menu.AddItem() as the on parameter. See Menu Item 2 in the example.