Unity Editor编辑器(MenuItem)
发表于2017-12-28
在菜单栏中添加按钮是为了方便大家进行编程,这里给大家总结的Editor编辑器经验,是由7种添加按钮的方法,分享给大家。
注意:所有Editor的脚本不需要挂载,只需要放在Editor文件夹下,Unity会自动编译,所有方法都是静态方法。
public MenuItem(string itemName);
public MenuItem(string itemName, bool isValidateFunction);
public MenuItem(string itemName, bool isValidateFunction, int priority);
Parameters
itemName | The itemName is the menu item represented like a pathname. For example the menu item could be "GameObject/Do Something".itemName是表示为路径名的菜单项。 例如,菜单项可以是“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 .如果isValidateFunction为true,那么这是一个验证函数,将在调用具有相同itemName的菜单函数之前被调用。 |
priority | The order by which the menu items are displayed. 菜单项的显示顺序。 |
1.在菜单栏添加按钮(如果添加到GameObject中的相应位置(修改priority)还可以在Hierarchy中右键点出)
[MenuItem("MyEditor/Mybutton",false,0)] public static void OnClick() { Debug.Log("按下button"); }
//必须在组件前面添加 CONTEXT组件标识 [MenuItem("CONTEXT/BoxCollider/TestCompent")] public static void Test() { Debug.Log("查看测试组件"); }
[MenuItem("CONTEXT/Rigidbody/ChangeRigidboby")] public static void ChangeRigidboby(MenuCommand cmd) { //获取当前组件 Rigidbody rig=cmd.context as Rigidbody; //修改组件相关变量 rig.mass=12; rig.useGravity = true; Debug.Log(cmd.context.name); Debug.Log("ChangeRigidbody"); }
注:(为字段添加特性)ContextMenuItem(“方法名”“按键名”),(为方法添加特性)ContextMenu(“按键名”)可以在组件内部为该组件添加button右键菜单按钮(详细可查API文档)
4.Selection 获取当前选中物体[MenuItem("GameObject/Mydelete",false,11)] public static void SeletionTest() { //打印当前选中物体的个数 Debug.Log(Selection.objects.Length); //如果你删除了你选中的物体,可以点击此按钮恢复这些物体 foreach (Object item in Selection.objects) { //可以撤销删除操作 Undo.DestroyObjectImmediate(item); } }
5.为按键添加快捷键
//%ctrl #shift &alt // ctrl+shift+w [MenuItem("MyButton/Debug %#w")] public static void MyButton1() { Debug.Log("Debug ctrl+shift+w"); }
6.方法验证
//为下方SeletionTest方法的验证 [MenuItem("GameObject/MyDelete",true,11)] public static bool MySeletionCondition() { //如果选中的物体不为空,则启用MyDelete按键 if(Selection.objects.Length!=0) { return true; } else { return false; } } //两个名称要一至,上方的true为开启验证 [MenuItem("GameObject/MyDelete", false,11)] public static void SeletionTest1() { //Selection 包含选中的一些方法,例如:ActionObject //输出选中的物体的个数(包含Project面板的物体) Debug.Log(Selection.objects.Length); //删除所有选中的物体(Unity回收机制类似回收站,删除的物体统一到一个地方) //当删除的物体达到一定程度时统一删除 foreach(Object item in Selection.objects) { DestroyObject(item); } }
7.创建与使用对话框
7.1创建对话框 例
/// <summary> /// 所有Editor的脚本不需要挂载,只需要放在Editor文件夹下,Unity会自动编译,所有方法都是静态方法 /// </summary>
//需要继承ScriptableWizard public class MyEditorTest : ScriptableWizard { //对话框中显示的值 public float aValue = 10.3f; public int bValue = 20; //按下那个键是创建 [MenuItem("MyTools/Create")] static void CreatWizard() { //创建对话框《创建类》(“对话框名称”) ScriptableWizard.DisplayWizard<MyEditorTest>("改变MyEditorTest脚本内部的值"); } }
执行效果:
7.2 使用 例
//unity内部方法,当按下对话框中Create键时执行(修改按键名称之后还是该方法) private void OnWizardCreate() { //获取到所有选中的Prefab GameObject[] objects = Selection.gameObjects; foreach(GameObject obj in objects) { //当按下按键是,修改他们所有的相应值 obj.GetComponent<MyEditorTest>().aValue += 12; obj.GetComponent<MyEditorTest>().bValue+=12; } }
OK,添加这7种按钮方法,希望能给大家有一点帮助