[SOLVED]Adding context menus to game object

I need to add a context menu to object within hierarchy window.

None of those seem to work.

public class TestMenu{
    [MenuItem("CONTEXT/GameObject/Test1")]
    static void testFunc(){
    }

    [MenuItem("CONTEXT/RigidBody/Test2")]
    static void testFunc2(){
    }

    [MenuItem("GameObject/Test3")]
    static void testFunc3(){
    }

    [MenuItem("CONTEXT/GameObject/Test4")]
    static void testFunc4(MenuCommand menuCommand){
    }
}

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.

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

public class MyTest{
//You can see this under GameObject/UI
//Grouped together with the UI components
    [MenuItem("GameObject/UI/Text Area", false, 10)]
    public static void CreateTextArea(){
        GameObject go = new GameObject("Name");
    }
//You can see this under GameObject
    [MenuItem("GameObject/My Custom Contex Menu Item 1/Subitem 1", false, 10)]
    public static void CreateTextArea2(){
        GameObject go = new GameObject("Name");
    }

    [MenuItem("GameObject/My Custom Contex Menu Item 1/Subitem 2", false, 10)]
    public static void CreateTextArea3(){
        GameObject go = new GameObject("Name");
    }

    [MenuItem("GameObject/My Custom Contex Menu Item 2", false, 10)]
    public static void CreateTextArea5(){
        GameObject go = new GameObject("Name");
    }
}

//Will add a Test->MyItem on the Add Component button on inspector
[AddComponentMenu("Test/MyItem")]
public class MyComponentTest : MonoBehaviour {

//Right clicking on this component in the inspector will show this
    [ContextMenu ("Context Menu for my Component")]
    void DoSomething () {
        Debug.Log ("Perform something");
    }
}
4 Likes

So, nobody knows?

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.

1 Like

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:

    [MenuItem("GameObject/Test9", false, 0)]
    static void testFunc9(){
    }

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.

17 Likes