itemName | The itemName is the menu item represented like a pathname.
For example the menu item could be "GameObject/Do Something". |
isValidateFunction | If isValidateFunction is true, this is a validation
function and will be called before invoking the menu function with the same itemName . |
priority | The order by which the menu items are displayed. |
Creates a menu item and invokes the static function following it, when the menu item is selected.
MenuItem
is an attribute that precedes a script function. This makes the function
appear in the Unity menu system. The menu location is specified by the itemName
argument.
isValidateFunction
is used to make a MenuItem
function as one that will be executed
before a script function with the same itemName
argument. The second argument is
boolean. If this argument is set to true
it will mark the associated function
as one that is called before the execution of the second script function. This second
script function with the same itemName
will be executed next.
priority
determines how the following script function is ordered in the menu system.
The integer value is compared against values on other script functions. If the
integer value is greater than the other values, then the MenuItem
script function will
be placed at the bottom of the list. The value of priority
can also be used to
manage the list of script functions into groups. The example later in this
page describes more about this feature.
The following script example adds two functions into a Example menu system.
using UnityEngine; using UnityEditor; using System.Collections;
public class ExampleClass : MonoBehaviour { // Add Example1 into a new menu list [MenuItem("Example/Example1", false, 100)] public static void Example1() { print("Example/Example1"); }
// Add Example2 into the same menu list [MenuItem("Example/Example2", false, 100)] public static void Example2() { print("Example/Example2"); } }
This simple following example shows how the Example
menu can have two entries divided by
a separator line. This happens when the priority
argument is separated by more
than 10. (However, see the following description.)
using UnityEngine; using UnityEditor; using System.Collections;
public class ExampleClass : MonoBehaviour { // Add Example1 has priority of 100 [MenuItem("Example/Example1", false, 100)] public static void Example1() { print("Example/Example1"); }
// Example2 has a priority of 111 which is 11 more than Example1. // This will cause a divider to be created. [MenuItem("Example/Example2", false, 111)] public static void Example2() { print("Example/Example2"); } }
Note: The understanding of ten or greater is considered to create a divider
in the menu. However, as per the example above, the difference between script
function need to have the priority
separated by 11 or more. This is why the
example before has a value of 100 and one of 111. Changing 111 to 110 does
not have a divider.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.