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:
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.