MenuItem
Creates a menu item and invokes the static function that follows it when the menu item is selected.
Read time 8 minutesLast updated 4 days ago
Definition
- Type: Constructor
- Namespace: UnityEditor
- Assembly: UnityEditor
MenuItem(string)
Creates a menu item and invokes the static function that follows it when the menu item is selected.
public MenuItem(string itemName)
Parameters
The is the menu item represented like a pathname. For example, the menu item could be .
itemName"GameObject/Do Something"Remarks
MenuItemitemNameisValidateFunctionMenuItemitemNametrueitemNameWarning: is only considered in the execution function when is set to false. When is set to true to mark a validation function, is not used.
priorityisValidateFunctionisValidateFunctionprioritypriorityMenuItempriorityThe following example code adds three menu items with corresponding functions to a menu called "Example": (priority 100), (priority 101), and then (priority 112). In the menu, a separator line divides from the other two menu items because the priority value of is 10+ greater than .
Example2Example1Example3Example3Example3Example1// Add menu item Example1 into a new menu called Example.[MenuItem("Example/Example1", false, 101)]public static void Example1(){ Debug.Log("Example/Example1");}// Add Example2 to the Example menu. Example2 has a priority lower than Example1, so it is added before Example1 in the menu.[MenuItem("Example/Example2", false, 100)]public static void Example2(){ Debug.Log("Example/Example2");}// Example3 has a priority of 112, which is 11 more than Example1.// This creates a divider between the two in the menu.[MenuItem("Example/Example3", false, 112)]public static void Example3(){ Debug.Log("Example/Example3");}
The following example includes validation functions. Do not use the parameter on the attribute of the validation function.
priority// The Example/SelectionCount menu item is enabled only when you select at least one object.[MenuItem("Example/SelectionCount", true)]public static bool SelectionCountValidation(){ return Selection.count > 0;}// The actual function that is called if the active menu item is selected.[MenuItem("Example/SelectionCount", false, 200)]public static void SelectionCountAction(){ Debug.Log($"The selection contains {Selection.count} element(s)");}
MenuItem(string, bool)
Creates a menu item and invokes the static function that follows it when the menu item is selected.
public MenuItem(string itemName, bool isValidateFunction)
Parameters
Remarks
MenuItemitemNameisValidateFunctionMenuItemitemNametrueitemNameWarning: is only considered in the execution function when is set to false. When is set to true to mark a validation function, is not used.
priorityisValidateFunctionisValidateFunctionprioritypriorityMenuItempriorityThe following example code adds three menu items with corresponding functions to a menu called "Example": (priority 100), (priority 101), and then (priority 112). In the menu, a separator line divides from the other two menu items because the priority value of is 10+ greater than .
Example2Example1Example3Example3Example3Example1// Add menu item Example1 into a new menu called Example.[MenuItem("Example/Example1", false, 101)]public static void Example1(){ Debug.Log("Example/Example1");}// Add Example2 to the Example menu. Example2 has a priority lower than Example1, so it is added before Example1 in the menu.[MenuItem("Example/Example2", false, 100)]public static void Example2(){ Debug.Log("Example/Example2");}// Example3 has a priority of 112, which is 11 more than Example1.// This creates a divider between the two in the menu.[MenuItem("Example/Example3", false, 112)]public static void Example3(){ Debug.Log("Example/Example3");}
The following example includes validation functions. Do not use the parameter on the attribute of the validation function.
priority// The Example/SelectionCount menu item is enabled only when you select at least one object.[MenuItem("Example/SelectionCount", true)]public static bool SelectionCountValidation(){ return Selection.count > 0;}// The actual function that is called if the active menu item is selected.[MenuItem("Example/SelectionCount", false, 200)]public static void SelectionCountAction(){ Debug.Log($"The selection contains {Selection.count} element(s)");}
MenuItem(string, bool, int)
Creates a menu item and invokes the static function that follows it when the menu item is selected.
public MenuItem(string itemName, bool isValidateFunction, int priority)
Parameters
The is the menu item represented like a pathname. For example, the menu item could be .
itemName"GameObject/Do Something"If is true, this is a validation function and is called before the menu function with the same is invoked.
isValidateFunctionitemNameThe order to display the menu item in a menu. If a menu item has a priority value that is 10 or greater than the item that precedes it, a separator line divides the two menu items from each other in the menu.
Remarks
MenuItemitemNameisValidateFunctionMenuItemitemNametrueitemNameWarning: is only considered in the execution function when is set to false. When is set to true to mark a validation function, is not used.
priorityisValidateFunctionisValidateFunctionprioritypriorityMenuItempriorityThe following example code adds three menu items with corresponding functions to a menu called "Example": (priority 100), (priority 101), and then (priority 112). In the menu, a separator line divides from the other two menu items because the priority value of is 10+ greater than .
Example2Example1Example3Example3Example3Example1// Add menu item Example1 into a new menu called Example.[MenuItem("Example/Example1", false, 101)]public static void Example1(){ Debug.Log("Example/Example1");}// Add Example2 to the Example menu. Example2 has a priority lower than Example1, so it is added before Example1 in the menu.[MenuItem("Example/Example2", false, 100)]public static void Example2(){ Debug.Log("Example/Example2");}// Example3 has a priority of 112, which is 11 more than Example1.// This creates a divider between the two in the menu.[MenuItem("Example/Example3", false, 112)]public static void Example3(){ Debug.Log("Example/Example3");}
The following example includes validation functions. Do not use the parameter on the attribute of the validation function.
priority// The Example/SelectionCount menu item is enabled only when you select at least one object.[MenuItem("Example/SelectionCount", true)]public static bool SelectionCountValidation(){ return Selection.count > 0;}// The actual function that is called if the active menu item is selected.[MenuItem("Example/SelectionCount", false, 200)]public static void SelectionCountAction(){ Debug.Log($"The selection contains {Selection.count} element(s)");}