itemName |
itemName 是指表示方式类似于路径名的菜单项。
例如,菜单项可能为“GameObject/Do Something”。 |
isValidateFunction | 如果 isValidateFunction 为 true,它将表示一个验证
函数,并在系统调用具有相同 itemName 的菜单函数之前进行调用。 |
priority | 菜单项显示的顺序。 |
创建一个菜单项并在选中此菜单项后调用静态函数。
MenuItem
是脚本函数前面的属性。此属性能够使该函数
显示在 Unity 菜单系统中。菜单位置由 itemName
参数指定。
isValidateFunction
用于使 MenuItem
函数作为将在
具有相同 itemName
参数的脚本函数之前执行的函数。第二个参数为
布尔值。如果该参数被设置为 itemName
,它将使相关联的函数
作为在执行第二个脚本函数之前调用的函数。具有
相同 itemName
的第二个函数将会随后执行。
priority
决定了以下脚本函数在菜单系统中的顺序。
整数值将会与其他脚本函数上的值进行比较。如果
该整数值大于其他值,那么 MenuItem
脚本函数将会
被放在该列表的底部。priority
的值也可用于
对脚本函数列表进行分组管理。此页面后面的示例
对此功能有详细描述。
以下脚本示例将两个函数添加到了示例菜单系统中。
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 将
不会产生分隔栏。
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.