Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Feedback Hook into the Scene context-menu to add custom items

Discussion in 'Editor & General Support' started by Peter77, Dec 15, 2018.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,626
    How can I hook into the Scene context-menu, to add custom items? Below you can find a screenshot of the context-menu I'm talking about.

    I want to add a few items there that run Code for the scene where the user opened the context menu for (which I need because we work a lot with a multi-scene setup).

    screenshot.png
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    You can check out the code that builds this context menu here (it's so simple now that this code is open sourced).

    This menu is generated by instantiating a GenericMenu instance and then displaying it as a context menu.
    I don't think you can interact with that to add your custom items into it.
     
    Peter77 likes this.
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,626
  4. bagle45

    bagle45

    Joined:
    Jul 25, 2016
    Posts:
    17
    For those still looking for this feature, there is a completely undocumented event that will let you do this without reflection.
    Code (CSharp):
    1.  
    2. UnityEditor.SceneManagement.SceneHierarchyHooks.addItemsToSceneHeaderContextMenu
    3.   += (menu, scene) => menu.AddItem (
    4.     new GUIContent("Scene Menu Item"),
    5.     false,
    6.     () =>
    7.     {
    8.       /*Your code here*/
    9.     });
    10.  
     
    nratcliff, Peter77 and spiney199 like this.