Adding to the context menu of the hierarchy tab?

I am trying to add a context menu when I right click on a gameobject in the scene (from the hierarchy), I am able to perform some actions on it.

I have tried using MenuItem("CONTEXT/..") but I have no idea what is the full string I should be using to specify the hierarchy context menu.

Any suggestions?

Edit: Here's the menu I wish to add to:

alt text

If you do: [MenuItem(“GameObject/MyMenu/Do Something”, false, 0)]

It will create a context menu accessible from the hierarchy when you right click on an object

Confirming that with Unity 3.5.7 (Windows) this works ONLY when right-clicking the component name label in the inspector.

Legend:

MenuItem(“CONTEXT/A/B”)

A - string type of your component (“Transform”, “GameObject”, “YourCustomComponent” etc.)

B - your command name. Seems it doesn’t work with nesting (like “My Component/Actions/Whatever”) - in fact it shows the triangle to the right, but doesn’t show the menu item.

It works with inheritance, meaning that if you extend YourCustomComponent, it will work for that component too.

CONTEXT is a constant that should be written in capital letters (this is all a bit dirty, right?)

Danko, 4/10/2013

Edit: in Unity 4.6+, gstock’s answer above is much better!

Here’s what I found. Basically registering for the EditorApplication.hierarchyWindowItemOnGUI callback from a static class then figuring out which object was clicked:

    using UnityEngine;
    using UnityEditor;
    using System.Collections;
    
    [InitializeOnLoad]
    public class CustomContextMenu
    {
    	static bool _MenuOpened = false;
    	static GameObject _ClickedOBject = null;
    	static Vector2 _MenuPosition;

    	static CustomContextMenu()
    	{
    		EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
    	}
    
    	static void OnHierarchyGUI(int instanceID, Rect selectionRect)
        {
    		// Whether this object was right clicked
    		if (Event.current != null && selectionRect.Contains(Event.current.mousePosition)
    			&& Event.current.button == 1 && Event.current.type <= EventType.mouseUp)
    		{
    			// Find what object this is
    			GameObject clickedObject = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
    
    			if (clickedObject)
    			{
    				//Debug.Log("Clicked " + clickedObject.name);
					_ClickedOBject = clickedObject;
    				_MenuPosition = Event.current.mousePosition;
    				_MenuOpened = true;

    				// Consume the event to remove Unity's default context menu
    				Event.current.Use();
    			}
    		}

    		if (_MenuOpened)
    		{
    			if (GUI.Button(new Rect(_MenuPosition.x, _MenuPosition.y, 150, 20f), "Delete"))
				{
					_MenuOpened = false;
					GameObject.Destroy(_ClickedObject);
				}
    		}
    	}
    }

From there you’ll have to handle plenty of different events (single clicks, focus change, key down, …) to know when to close the menu.

I'm trying to do something similar. I'd like to be able to select 2 or more GOs from the hierarchy and right click to perform a script on the array of GOs selected... but that doesn't seem to be an option from what I can tell.

No multiselection indeed wouldn't work with the context menu, thats something for which you would need an editor window for example where Selection is being used which contains a list of objects selected

vote for this
http://feedback.unity3d.com/unity/all-categories/1/hot/active/add-onhierarchyviewgui-to-draw-g