Search Unity

[SOLVED]Adding context menus to game object

Discussion in 'Scripting' started by neginfinity, Jun 10, 2016.

Thread Status:
Not open for further replies.
  1. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    I need to add a context menu to object within hierarchy window.

    None of those seem to work.
    Code (csharp):
    1.  
    2. public class TestMenu{
    3.     [MenuItem("CONTEXT/GameObject/Test1")]
    4.     static void testFunc(){
    5.     }
    6.  
    7.     [MenuItem("CONTEXT/RigidBody/Test2")]
    8.     static void testFunc2(){
    9.     }
    10.  
    11.     [MenuItem("GameObject/Test3")]
    12.     static void testFunc3(){
    13.     }
    14.  
    15.     [MenuItem("CONTEXT/GameObject/Test4")]
    16.     static void testFunc4(MenuCommand menuCommand){
    17.     }
    18. }
    19.  
    Except for testFunc3 which adds a menu to MAIN menu of Unity editor.
    I'm positive that this worked in the past.

    Ideas? Unity version 5.3.3f1.

    https://docs.unity3d.com/ScriptReference/MenuItem.html
     
  2. coffeekitkat

    coffeekitkat

    Joined:
    May 17, 2015
    Posts:
    3
    works fine in my case, i can see my context menu on both Main Menu on Unity editor and on Hierarchy
    here are some examples i've done.
    i tried Menutem and the ContextMenu attributes i hope this is the one you are looking for
    Code (CSharp):
    1. public class MyTest{
    2. //You can see this under GameObject/UI
    3. //Grouped together with the UI components
    4.     [MenuItem("GameObject/UI/Text Area", false, 10)]
    5.     public static void CreateTextArea(){
    6.         GameObject go = new GameObject("Name");
    7.     }
    8. //You can see this under GameObject
    9.     [MenuItem("GameObject/My Custom Contex Menu Item 1/Subitem 1", false, 10)]
    10.     public static void CreateTextArea2(){
    11.         GameObject go = new GameObject("Name");
    12.     }
    13.  
    14.     [MenuItem("GameObject/My Custom Contex Menu Item 1/Subitem 2", false, 10)]
    15.     public static void CreateTextArea3(){
    16.         GameObject go = new GameObject("Name");
    17.     }
    18.  
    19.     [MenuItem("GameObject/My Custom Contex Menu Item 2", false, 10)]
    20.     public static void CreateTextArea5(){
    21.         GameObject go = new GameObject("Name");
    22.     }
    23. }
    24.  
    25. //Will add a Test->MyItem on the Add Component button on inspector
    26. [AddComponentMenu("Test/MyItem")]
    27. public class MyComponentTest : MonoBehaviour {
    28.  
    29. //Right clicking on this component in the inspector will show this
    30.     [ContextMenu ("Context Menu for my Component")]
    31.     void DoSomething () {
    32.         Debug.Log ("Perform something");
    33.     }
    34. }
     
    Last edited: Jun 13, 2016
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    So, nobody knows?
     
  4. Cynikal

    Cynikal

    Joined:
    Oct 29, 2012
    Posts:
    122
    Sometimes when you add a custom menu ("CONTEXT") the editor UI has to update itself.

    You can achieve that by reimporting the asset that has the code in it, or just simply restarting Unity.

    This happens when you uninstall assets, how the menu items and info will still remain. GameObject is already a menu item on unity, so it's easy to tie in.
     
    neginfinity likes this.
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    That's not the case. Menus appear instantly.

    Either way I finally figured it out.

    For menu to appear in hierarchy tab, its function must be declared as:
    Code (csharp):
    1.  
    2.     [MenuItem("GameObject/Test9", false, 0)]
    3.     static void testFunc9(){
    4.     }
    5.  
    3rd field - priority - must be present and must have priority less than 49, "because reasons".

    Using "CONTEXT/ObjectType/MenuName" will cause it to appear in rightclick menu within inspector (meaning, it'll show up when you right click component title in inspector, which is close to useless).

    Also, it is possible to add menus to asset list, via "Assets/MenuName" paths.

    Problem solved, unsubscribing.
     
    _geo__, L0tan, dan-oak and 14 others like this.
Thread Status:
Not open for further replies.