MenuItemConstructor

切换到手册
public MenuItem (string itemName);
public MenuItem (string itemName, bool isValidateFunction);
public MenuItem (string itemName, bool isValidateFunction, int priority);

参数

itemName itemName 是指表示方式类似于路径名的菜单项。 例如,菜单项可能为“GameObject/Do Something”。
isValidateFunction如果 isValidateFunction 为 true,它将表示一个验证 函数,并在系统调用具有相同 itemName 的菜单函数之前进行调用。
priority菜单项显示的顺序。

描述

创建一个菜单项并在选中此菜单项后调用静态函数。

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 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"); } }

下面这个简单的示例显示了 Example 菜单如何用分割线分开 两个条目。当 priority 参数之间所间隔的级别 超过 10 个级别时,会发生这种情况。(具体请参阅以下描述。)

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"); } }

注意:10 或更大值会被理解成能够在菜单中 创建一个分隔栏。然而,根据上面的示例来看,脚本函数 之间的差异需要使 priority 之间所间隔的级别为 11 或更大值。这就是 为什么在上面的示例中一个值为 100 而另一个值 111。将 111 更改为 110 将 不会产生分隔栏。