Quick tutorial: How to add to the custom menu of an editor window

Edit from the year 2022: People still find this thread regularly, take note that this feature is officially documented starting with Unity 2018: Unity - Scripting API: IHasCustomMenu

I’ve been wondering if I could add my own content to the custom menu that is built into every editor window. I’m still not sure, how that dropdown menu is called, but this is the one I mean:

3309553--256996--EditorWindowCustomMenu.png

Luckily, today I stumbled across the solution and wanted to share it since I couldn’t find much about it online.

using UnityEngine;
using UnityEditor;

public class MyWindow : EditorWindow, IHasCustomMenu
{
    [MenuItem("Window/MyWindow")]
    private static void ShowWindow()
    {
        GetWindow<MyWindow>().Show();
    }

    // This interface implementation is automatically called by Unity.
    void IHasCustomMenu.AddItemsToMenu(GenericMenu menu)
    {
        GUIContent content = new GUIContent("My Custom Entry");
        menu.AddItem(content, false, MyCallback);
    }

    private void MyCallback()
    {
        Debug.Log("My Callback was called.");
    }
}

To add your own custom entries, just add the IHasCustomMenu interface from the UnityEditor namespace to your EditorWindow class and implement it as shown in the sample.

You can, of course just do this:

public void AddItemsToMenu(GenericMenu menu)
{
    // ...
}

but I prefer the explicit interface because this method is one of those, which should only be called by Unity, not by my own code and the explicit declaration reminds me of the fact.

And finally, how did I find this functionality (since it is not mentioned in the Manual or the Scripting API)? I’ve looked at the decompiled UnityEditor.dll (the C# side of the engine, mostly concerned with the Editor API for drawing windows) and searched for the string “Stack Trace Logging” because I wondered how the Console window implemented this functionality and if I could add my own entries next to it.

Disclaimer: The IHasCustomMenu is not documented and so it is not officially supported and may not even work as I advertise. Additionally, it is not allowed to decompile Unity code, but there are some sources online which indicate, that the Unity staff is mostly ok with looking at editor code for educational purposes like this one.

Edit: Please, if anyone knows, tell me how this built-in custom menu is called and maybe I can update the title to make it more obvious that I’m not talking about user-generated custom menus.

16 Likes

Good tutorial! :slight_smile: Your understanding of IHasCustomMenu is correct. It’s on our todo-list to add proper API for this, but just hasn’t happened yet.

Yep, good disclaimer. With that said, it’s perfectly fine to decompile Unity’s managed code.
From https://unity3d.com/legal/terms-of-service/software

We just call it the EditorWindow’s Context Menu. Though it doesn’t really have an official name until there’s proper API for it.

1 Like

Thanks for the response! I do like the way custom extensions are handled with the Context Menu, would be nice to see more of this type of open design. :slight_smile:

And thanks for linking those service terms, I didn’t know that there was an official statement about decompiling editor code.

@Xarbrough_1 man… I don’t have the words to express how HAPPY I am to have found this. I just spent a week learning relfection (had never touched it) trying to figure out how to control the docking of windows and tab positioning… finaly got it to work but man the learning curve!

I can finally have a headache free day!

Thank you so much for posting your code :smile:

EDIT: Correction, I can have half a day headache free. Now I gotta figure how to remove something from that menu :face_with_spiral_eyes: You wouldn’t happen to know were the generic menu of an editor window is stored?

1 Like

Glad I could help. Do you want to remove entries, which you added yourself to the menu or do you want to remove builtin entries added by Unity? I believe the latter case is rather difficult to change. It looks like all entries are added by Unity in place before the window is displayed and the data is not retained, so cannot be changed without changing the actual Unity source code, but maybe I’m wrong.

awesome! thank you ^^