Game Development Reference
In-Depth Information
Adding contextual MenuItems
The last bit of trickery you can perform is to add menu items to the existing features of
Unity, even Inspector .
You do this with a custom name for the MenuItem attribute and a different signature for
the function. So, we add the following method to our MyMenu script:
[MenuItem("CONTEXT/Transform/Move to Center")]
static void MoveToCenter(MenuCommand command)
{
Transform transform = (Transform)command.context;
transform.position = Vector3.zero;
Debug.Log("Moved object to " +
transform.position + " from a Context Menu.");
}
The preceding script attaches itself to any transform component (in this case, the Inspect-
or pane). Then, when it is run, the parameter on the function receives the instance of the
object it was run on and lets you interrogate or alter it, resulting in the following screen-
shot:
Search WWH ::




Custom Search