Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

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

Discussion in 'Immediate Mode GUI (IMGUI)' started by Xarbrough, Dec 3, 2017.

  1. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    Edit from the year 2022: People still find this thread regularly, take note that this feature is officially documented starting with Unity 2018: https://docs.unity3d.com/ScriptReference/IHasCustomMenu.html


    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:

    EditorWindowCustomMenu.png

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

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class MyWindow : EditorWindow, IHasCustomMenu
    5. {
    6.     [MenuItem("Window/MyWindow")]
    7.     private static void ShowWindow()
    8.     {
    9.         GetWindow<MyWindow>().Show();
    10.     }
    11.  
    12.     // This interface implementation is automatically called by Unity.
    13.     void IHasCustomMenu.AddItemsToMenu(GenericMenu menu)
    14.     {
    15.         GUIContent content = new GUIContent("My Custom Entry");
    16.         menu.AddItem(content, false, MyCallback);
    17.     }
    18.  
    19.     private void MyCallback()
    20.     {
    21.         Debug.Log("My Callback was called.");
    22.     }
    23. }
    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:

    Code (CSharp):
    1. public void AddItemsToMenu(GenericMenu menu)
    2. {
    3.     // ...
    4. }
    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.
     
    Last edited: Apr 6, 2022
    Johannski, t2g4, georev and 11 others like this.
  2. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
    Good tutorial! :) 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.
     
    Xarbrough likes this.
  3. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    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. :)

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

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    @Xarbrough 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 :D


    EDIT: Correction, I can have half a day headache free. Now I gotta figure how to remove something from that menu :confused: You wouldn't happen to know were the generic menu of an editor window is stored?
     
    Last edited: Apr 20, 2019
    Smireles likes this.
  5. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    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.
     
  6. nehvaleem

    nehvaleem

    Joined:
    Dec 13, 2012
    Posts:
    434
    awesome! thank you ^^