MenuItem Manual     Reference     Scripting  
Scripting > Editor Classes > MenuItem
MenuItem Class, inherits from System.Attribute

The MenuItem attribute allows you to add menu items to the main menu and inspector context menus.

Note: This is an editor class. To use it you have to place your script in Assets/Editor inside your project folder. Editor classes are in the UnityEditor namespace so for C# scripts you need to add "using UnityEditor;" at the beginning of the script.

The MenuItem attribute turns any static function into a menu command. Only static functions can use the MenuItem attribute.

To create a hotkey you can use the following special characters: % (ctrl on Windows, cmd on OS X), # (shift), & (alt), _ (no key modifiers). For example to create a menu with hotkey shift-alt-g use "GameObject/Do Something #&g". To create a menu with hotkey g and no key modifiers pressed use "GameObject/Do Something _g". A hotkey text must be preceded with a space character ("GameObject/Do_g" won't be interpreted as hotkey, while "GameObject/Do _g" will).

// JavaScript example:
// Add menu named "Do Something" to the main menu
@MenuItem ("GameObject/Do Something")
static function DoSomething () {
Debug.Log ("Perform operation");
}

// Validate the menu item.
// The item will be disabled if no transform is selected.
@MenuItem ("GameObject/Do Something", true)
static function ValidateDoSomething () {
return Selection.activeTransform != null;
}

// Add menu named "Do Something" to the main menu
// and give it a shortcut (ctrl-o on Windows, cmd-o on OS X).
@MenuItem ("GameObject/Do Something %o")
static function DoSomething () {
Debug.Log ("Perform operation");
}

// Add context menu named "Do Something" to rigid body's context menu
@MenuItem ("CONTEXT/Rigidbody/Do Something")
static function DoSomething (command:MenuCommand) {
var body : Rigidbody = command.context;
body.mass = 5;
}

// C# example:
using UnityEditor;
using UnityEngine;
class MenuTest : MonoBehaviour {
// Add menu named "Do Something" to the main menu
[MenuItem ("GameObject/Do Something")]
static void DoSomething () {
Debug.Log ("Perform operation");
}

// Validate the menu item.
// The item will be disabled if no transform is selected.
[MenuItem ("GameObject/Do Something", true)]
static bool ValidateDoSomething () {
return Selection.activeTransform != null;
}

// Add menu named "Do Something" to the main menu
// and give it a shortcut (ctrl-o on Windows, cmd-o on OS X).
[MenuItem ("GameObject/Do Something %o")]
static void DoSomething () {
Debug.Log ("Perform operation");
}

// Add context menu named "Do Something" to rigid body's context menu
[MenuItem ("CONTEXT/Rigidbody/Do Something")]
static void DoSomething (MenuCommand command) {
Rigidbody body = (Rigidbody)command.context;
body.mass = 5;
}
}

Constructors
MenuItem

Creates a menu item and invokes the static function following it, when the menu item is selected.