最简单的用法:
[MenuItem(
"Tools/ClearPlayerPrefs"
)]
此外,还有很多用法。
添加快捷键
- %-CTRL 在Windows / CMD在OSX
- # -Shift
- & -Alt
- LEFT/RIGHT/UP/DOWN-光标键
- F1…F12
- HOME,END,PGUP,PDDN
注:字母键不是key-sequence的一部分,要让字母键被添加到key-sequence中必须在前面加上下划线(例如:_g对应于快捷键”G”)。
[MenuItem(
"Tools/New Option %#a"
)]即Alt+Shift+A
[MenuItem(
"Tools/Item2 _g"
)]即G
特殊路径
- Assets -添加到“Assets”菜单下,同时也显示在右键单击项目视图时弹出的菜单中。
- Asset/Create - 添加在“Assets ->Create”子菜单中。
- CONTEXT/ComponentName - 菜单项将出现在给定组件的(右键单击显示的菜单)中,即复位,复制组件的那个菜单里。
有效验证
有些菜单项仅在指定的情况下才可用,否则就应该被禁用。根据它的上下文添加一个验证方法来启用/禁用一个菜单项。
- 验证方法是一个静态的,并使用MenuItem属性标记的一个方法,且此属性传递true作为一个验证参数。
- 这个验证方法应该和菜单的命令方法有相同的路径,并且要有一个boolean的返回值,用以确认菜单是否被激活或者禁用。
//菜单
[MenuItem(
"Assets/ProcessTexture"
)]
private
static
void
DoSomethingWithTexture
() {
}
//以下是该方法的验证方法,用于是否禁用
[MenuItem(
"Assets/ProcessTexture"
,
true
)]//当返回真时启用
private
static
bool
NewMenuOptionValidation
() {
return
Selection.activeObject.GetType() ==
typeof
(Texture2D);//如果所选择的不是
Texture2D类型,则返回false
}
菜单的顺序
优先级是个被赋值到菜单项一个数字(传递给MenuItemde的第3个参数),它控制了菜单的显示顺序。
菜单项也能够自动的分组,每50个一个组:
[MenuItem(
"NewMenu/Option1"
,
false
,
1
)]
[MenuItem(
"NewMenu/Option2"
,
false
,
2
)]
[MenuItem(
"NewMenu/Option3"
,
false
,
3
)]
[MenuItem(
"NewMenu/Option4"
,
false
,
51
)]
[MenuItem(
"NewMenu/Option5"
,
false
,
52
)]
MenuCommand
当添加一个新的菜单项到Inspector中(使用 “CONTEXT/Component”),有时候获取一个真实组件的引用也是有必要的(例如:修改它的数据)。
这可以通过给新菜单项的静态方法添加一个MenuCommand参数。
[
@MenuItem
(
"CONTEXT/Rigidbody/Do Something"
)]
public static void DoSomething (MenuCommand command) {
Rigidbody body= command.context
as
Rigidbody
;
body.mass =
5
;
}
ContextMenu
这个属性允许你去定义上下文菜单。它和使用MenuItem并以“CONTEXT/…”作为路径定义菜单项一样。
这个属性不同的是,你能给指定的组件定义默认的上下文菜单项,然而使用MeneItem只是为了扩展现有的组件菜单,比如引擎默认的组件。
public
class
AddChild
:
MonoBehaviour
{
[
ContextMenu
(
"Do Something"
)]
void DoSomething () {
Debug .Log ( "Perform operation" );
}
void DoSomething () {
Debug .Log ( "Perform operation" );
}
}
注意:该脚本是标准脚本,该脚本添加到物体上后便可右键找到执行函数。
ContextMenuItem
这个属性被添加到组件类的字段上,上面显示了ContextMenu属性在组件上下文菜单中添加了一个菜单项,然后ContextMenuItem属性将在字段的上下文菜单中添加一个菜单项。
由于这个属性被添加到了一个字段上没有方法,所有它接受两个参数:一个用于显示菜单项的名字,一个用于指定一个当菜单被选中时要调用的实例方法。
public
class
ContextMenuItem
:
MonoBehaviour
{
[ContextMenuItem( "Randomize Name" , "Randomize" )]
public string Name;
private void Randomize()
{
Name = "Some Random Name" ;
}
![]()
[ContextMenuItem( "Randomize Name" , "Randomize" )]
public string Name;
private void Randomize()
{
Name = "Some Random Name" ;
}
}
注:第一个参数为菜单名,第二个是指定的函数。在字段name上点击(黄色部分)。
AddComponentMenu
此属性允许你去放置脚本到“Component”菜单下的任意位置,而不只是在“Component->Scripts”菜单下。
你能使用它去更好的组织你的脚本。这个属性能够改善添加脚本的工作流程。重要提示:你需要重启才生效。
using
UnityEngine;
using System.Collections;
[ContextMenuItem( "Randomize Name" , "Randomize" )]
public string Name;
private void Randomize()
{
Name = "Some Random Name" ;
}
![]()
using System.Collections;
[AddComponentMenu("Transform/ContextMenuItem")]//-------------------这里
public
class
ContextMenuItem
:
MonoBehaviour
{[ContextMenuItem( "Randomize Name" , "Randomize" )]
public string Name;
private void Randomize()
{
Name = "Some Random Name" ;
}
}
GenericMenu
GenericMenu让你可以创建一个自定义的上下文菜单和下拉菜单,下面这个示例打开一个带有绿色区域的窗口,在绿色区域上单击右键将显示一个菜单,当触发了菜单中选中的项时将触发一个回调。
using
UnityEngine;
using UnityEditor;
using System.Collections;
public
class
GenericMenuExample
:
EditorWindow
{
[ MenuItem ( "Example/Open Window" )]
static void Init()
{
var window = GetWindow< GenericMenuExample >();//绘制窗口
window.position = new Rect ( 50 , 50 , 250 , 60 );
window.Show();//显示该窗口
}
void Callback( object obj)
{
Debug .Log( "Selected: " + obj);
}
void OnGUI()
{
Event currentEvent = Event .current;//获取当前事件
Rect contextRect = new Rect ( 10 , 10 , 100 , 100 );
EditorGUI .DrawRect(contextRect, Color .green);
Vector2 mousePos = currentEvent.mousePosition;//当前鼠标的位置
if (contextRect.Contains(mousePos))//鼠标范围是在矩形内
{
// Now create the menu, add items and show it
menu.AddItem( new GUIContent ( "MenuItem2" ), false , Callback, "item 2" );
menu.AddSeparator( "" );//创建分隔
menu.AddItem( new GUIContent ( "SubMenu/MenuItem3" ), false , Callback, "item 3" );
menu.ShowAsContext();//当右键时显示
}
}
}
using UnityEditor;
using System.Collections;
{
[ MenuItem ( "Example/Open Window" )]
static void Init()
{
var window = GetWindow< GenericMenuExample >();//绘制窗口
window.position = new Rect ( 50 , 50 , 250 , 60 );
window.Show();//显示该窗口
}
void Callback( object obj)
{
Debug .Log( "Selected: " + obj);
}
void OnGUI()
{
Event currentEvent = Event .current;//获取当前事件
Rect contextRect = new Rect ( 10 , 10 , 100 , 100 );
EditorGUI .DrawRect(contextRect, Color .green);
if (currentEvent.type == EventType.ContextClick)//事件类型为在context上右键
{Vector2 mousePos = currentEvent.mousePosition;//当前鼠标的位置
if (contextRect.Contains(mousePos))//鼠标范围是在矩形内
{
// Now create the menu, add items and show it
GenericMenu
menu =
new
GenericMenu
();//
GenericMenu用来创建自定义上下文菜单和下拉菜单。
menu.AddItem(
new
GUIContent
(
"MenuItem1"
),
false
, Callback,
"item 1"
);//添加菜单项menu.AddItem( new GUIContent ( "MenuItem2" ), false , Callback, "item 2" );
menu.AddSeparator( "" );//创建分隔
menu.AddItem( new GUIContent ( "SubMenu/MenuItem3" ), false , Callback, "item 3" );
menu.ShowAsContext();//当右键时显示
}
}
}