MenuItem Class, inherits from System.AttributeScripting > Editor Classes > MenuItemThe 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: % (cmd), # (shift), & (alt), ^ (control), _ (no key modifiers). For example to create a menu with hotkey cmd-alt-g use "GameObject/Do Something %&g". To create a menu with hotkey g and no key modifiers pressed use "GameObject/Do Something _g". // 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 cmd-o @MenuItem ("GameObject/Do Something %o") static function DoSomething () { Debug.Log ("Perform operation"); } // Add context menu named "Do Something" to context menu @MenuItem ("CONTEXT/Rigidbody/Do Something") static function DoSomething (MenuCommand command) { 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 cmd-o [MenuItem ("GameObject/Do Something %o")] static void DoSomething () { Debug.Log ("Perform operation"); } // Add context menu named "Do Something" to context menu [MenuItem ("CONTEXT/Rigidbody/Do Something")] static void DoSomething (MenuCommand command) { Rigidbody body = (Rigidbody)command.context; body.mass = 5; } } Constructors
|