MenuItem的用法
2014-03-08 14:53
344 查看
1.
MenuItem是编辑器属性(Editor Attributes)类,所以在使用它之前要添加一句:using UnityEditor;
2.
常规用法:
MenuItem属性写在一个静态方法之前;
MenuItem必须定义子菜单,像这样写:[MenuItem("Delete")] 是不行的。
3.
给菜单项添加快捷键:
To create a hotkey you can use the following special characters: % (ctrl on Windows, cmd on OS X),# (shift),& (alt), <b>_</b> (no key modifiers). For example to create a menu with hotkey shift-alt-g use
添加菜单项的快捷键时,首先要输入一个空格,%在Windows中代表ctrl(OS X中代表cmd),#代表shift,&代表alt,_代表什么也没有。
如添加shift+alt+g快捷键时使用“MyMenu/Do Something #&g”, 添加g快捷键时使用"MyMenu/Do Someting _g"(必须按大写的G才起作用哦)
Some special keyboard keys are supported as hotkeys, for example "#LEFT" would map to shift-left. The keys supported like this are: LEFT, RIGHT, UP, DOWN, F1 .. F12, HOME, END, PGUP, PGDN.
快捷键还支持一些特殊的键,如“#LEFT”代表左shift。支持的键有:LEFT, RIGHT, UP, DOWN, F1 .. F12, HOME, END, PGUP, PGDN.
4.
在Inspector中添加上下文菜单,类似于ContextMenu属性。
写法为[MenuItem("CONTEXT/要添加菜单项的组件类型/菜单项名")]
在实现菜单命令的静态函数中要添加一个MenuCommand类型参数command
当点击菜单项时,菜单项所在的组件实例将传递给command,通过comman.context获取该组件的实例,从而对该组件进行操作。
5.
添加验证函数以选择是否启用该菜单项
假如有个如下所示的菜单项:
这是一个再普通不过的菜单项,目的是获取当前选择的物体的transform并输出它的名字。
当我们没有选择任何一个物体时,会报错。
所以我们要给该菜单项的启用添加限定条件:只有当前选择的物体的transform不为null时,才启用该菜单项。
方法为:
可以看到,第一行的写法,MenuItem中的菜单项名字完全与上面的一致,只是多了一个参数true.
另外写一个静态函数,只有该函数返回true时,相应的菜单项才能被启用。
只有当MenuItem中的第二个参数设为true时,才能成为验证函数。若该参数为false,等同于没有第二个参数,使得其下的静态函数成为菜单命令函数。
实际上这第二个参数的名字为bool isValidateFunction,也就是说是否为验证函数的意思。
6.
设置多个菜单项在菜单栏中的显示顺序
MenuItem(string
itemName, bool isValidateFunction, int
priority);
priority小的排在前面,大的排在后面。
注意,如果不希望菜单项成为验证函数,把isValidateFunction设置为false就可以了。否则是不会显示在菜单栏中的
MenuItem是编辑器属性(Editor Attributes)类,所以在使用它之前要添加一句:using UnityEditor;
2.
常规用法:
MenuItem属性写在一个静态方法之前;
MenuItem必须定义子菜单,像这样写:[MenuItem("Delete")] 是不行的。
using UnityEngine; using System.Collections; using UnityEditor; public class DelelteAll { [MenuItem("Delete/Delete All Keys")] static void DeleteAllKeys() { PlayerPrefs.DeleteAll(); } }
3.
给菜单项添加快捷键:
To create a hotkey you can use the following special characters: % (ctrl on Windows, cmd on OS X),# (shift),& (alt), <b>_</b> (no key modifiers). For example to create a menu with hotkey shift-alt-g use
"MyMenu/Do Something #&g". To create a menu with hotkeyg and no key modifiers pressed use
"MyMenu/Do Something _g".
添加菜单项的快捷键时,首先要输入一个空格,%在Windows中代表ctrl(OS X中代表cmd),#代表shift,&代表alt,_代表什么也没有。
如添加shift+alt+g快捷键时使用“MyMenu/Do Something #&g”, 添加g快捷键时使用"MyMenu/Do Someting _g"(必须按大写的G才起作用哦)
Some special keyboard keys are supported as hotkeys, for example "#LEFT" would map to shift-left. The keys supported like this are: LEFT, RIGHT, UP, DOWN, F1 .. F12, HOME, END, PGUP, PGDN.
快捷键还支持一些特殊的键,如“#LEFT”代表左shift。支持的键有:LEFT, RIGHT, UP, DOWN, F1 .. F12, HOME, END, PGUP, PGDN.
4.
在Inspector中添加上下文菜单,类似于ContextMenu属性。
写法为[MenuItem("CONTEXT/要添加菜单项的组件类型/菜单项名")]
在实现菜单命令的静态函数中要添加一个MenuCommand类型参数command
当点击菜单项时,菜单项所在的组件实例将传递给command,通过comman.context获取该组件的实例,从而对该组件进行操作。
// Add a menu item called "Double Mass" to a Rigidbody's context menu. [MenuItem ("CONTEXT/Rigidbody/Double Mass")] static void DoubleMass (MenuCommand command) { Rigidbody body = (Rigidbody)command.context; body.mass = body.mass * 2; Debug.Log ("Doubled Rigidbody's Mass to " + body.mass + " from Context Menu."); }
5.
添加验证函数以选择是否启用该菜单项
假如有个如下所示的菜单项:
这是一个再普通不过的菜单项,目的是获取当前选择的物体的transform并输出它的名字。
当我们没有选择任何一个物体时,会报错。
// Validated menu item. // Add a menu item named "Log Selected Transform Name" to MyMenu in the menu bar. // We use a second function to validate the menu item // so it will only be enabled if we have a transform selected. [MenuItem ("MyMenu/Log Selected Transform Name")] static void LogSelectedTransformName () { Debug.Log ("Selected Transform is on " + Selection.activeTransform.gameObject.name + "."); }
所以我们要给该菜单项的启用添加限定条件:只有当前选择的物体的transform不为null时,才启用该菜单项。
方法为:
// Validate the menu item defined by the function above. // The menu item will be disabled if this function returns false. [MenuItem ("MyMenu/Log Selected Transform Name", true)] static bool ValidateLogSelectedTransformName () { // Return false if no transform is selected. return Selection.activeTransform != null; }
可以看到,第一行的写法,MenuItem中的菜单项名字完全与上面的一致,只是多了一个参数true.
另外写一个静态函数,只有该函数返回true时,相应的菜单项才能被启用。
只有当MenuItem中的第二个参数设为true时,才能成为验证函数。若该参数为false,等同于没有第二个参数,使得其下的静态函数成为菜单命令函数。
实际上这第二个参数的名字为bool isValidateFunction,也就是说是否为验证函数的意思。
6.
设置多个菜单项在菜单栏中的显示顺序
MenuItem(string
itemName, bool isValidateFunction, int
priority);
priority小的排在前面,大的排在后面。
注意,如果不希望菜单项成为验证函数,把isValidateFunction设置为false就可以了。否则是不会显示在菜单栏中的
相关文章推荐
- 11. android 选项菜单(此处为icon menu)--Menu,SubMenu,MenuItem的用法
- unity编辑器(二)----MenuItem的用法
- unity3D 编辑器扩展,MenuItem 和 ContextMenu 的用法和分析
- JavaScript shift() Method 用法
- Deflater 和 Inflater 的用法
- linux chown与chgrp 的用法
- [精彩] volatile用法的相关文章(转)
- busybox简介及其简单用法
- BackGroundWorker用法
- C6000的线性汇编--格式,用法以及优化策略
- iterator的用法
- 类__slots__与__dict__用法
- matlab中size()函数的用法
- SQL中EXISTS的用法
- c++ ofstream和ifstream详细用法
- sql for xml path用法
- List,set,Map 的用法和区别等
- SQL count详细用法
- JQuery包装集size,length,index,slice,find,filter,is,children,next,nextAll,parent,parents,closest,siblings,add,end,andSelf的用法
- const的常见用法