首页 > 移动开发 > HTML5 > Unity 编辑器中原六仔搭建扩展
Editor中原六仔搭建 dsluntan.com Q:3393756370 VX:17061863513文件夹内的资源都不会被打包出去(包括PC 移动端等 只会在unity的编辑器模式运行)
//正常的添加菜单栏 [MenuItem("Tools/test")] static void Test() { Debug.Log("test");}
如果要添加 菜单栏的分割线
[MenuItem("Tools/test1", false, 1)] static void Test1() { Debug.Log("test1"); } [MenuItem("Tools/test2", false, 12)] static void Test2() { Debug.Log("test2"); } [MenuItem("Tools/test3",false ,0),] static void Test3() { Debug.Log("test3");}
第三个参数 priority 是1-1000 相当于层级 越小越靠上
相差大于11 就会出现 一条横杠
要想在Gameobject 菜单栏 添加子菜单
[MenuItem("GameObject/ My test",false,10)] static void Test() { Debug.Log("test2"); }
在Editor新建PlayerEditor脚本
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor;public class PlayerEditor {//CONTEXT 固定写法//PlayerHealth 脚本名(组件名)//InitHealthAndSpeed 按钮名称[MenuItem("CONTEXT/PlayerHealth/InitHealthAndSpeed")]static void InitHealthAndSpeed(){Debug.Log("Init");} }
MenuCommand 用法
//CONTEXT 固定写法//PlayerHealth 脚本名(组件)//InitHealthAndSpeed 按钮名称[MenuItem("CONTEXT/PlayerHealth/InitHealthAndSpeed")]static void InitHealthAndSpeed(MenuCommand menu) //MenuCommand 是当前正在操作的组件{Debug.Log(menu.context.name);Debug.Log("Init");}
修改PlayerHealth中的参数
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor;public class PlayerEditor {//CONTEXT 固定写法//PlayerHealth 脚本名(组件)//InitHealthAndSpeed 按钮名称[MenuItem("CONTEXT/PlayerHealth/InitHealthAndSpeed")]static void InitHealthAndSpeed(MenuCommand menu) //MenuCommand 是当前正在操作的组件{Debug.Log(menu.context.name);Debug.Log("Init");PlayerHealth ph = (PlayerHealth)menu.context;ph.name = "haha";} }
cube的名字就变成了haha
编写代码
[MenuItem("CONTEXT/Rigidbody/Clear")]static void ClearMassAdnGravity(MenuCommand cmd){Rigidbody rigidbody = (Rigidbody)cmd.context;rigidbody.mass = 0;rigidbody.useGravity = false;}
[MenuItem("Tools/test2", false, 12)]static void Test2(){Debug.Log(Selection.activeObject.name);//是我们选择的第一个游戏物体}
Selection 是一个静态的选择类
[MenuItem("Tools/test2", false, 12)]static void Test2(){// Debug.Log(Selection.activeObject.name);foreach (GameObject item in Selection.gameObjects){Debug.Log( item.name);Undo.DestroyObjectImmediate(item); //(能用 Ctrl+z 来撤销})))// GameObject.DestroyImmediate(item); }}
//%=ctrl # =shift &=alt[MenuItem("Tools/test2 %q", false, 12)]static void Test2(){// Debug.Log(Selection.activeObject.name);foreach (GameObject item in Selection.gameObjects){Debug.Log( item.name);Undo.DestroyObjectImmediate(item);// GameObject.DestroyImmediate(item); }}//%=ctrl # =shift &=alt[MenuItem("Tools/test3 _t" ,false ,0),] //快捷键为Tstatic void Test3(){Debug.Log("test3");}
只有选择的有物体的时候才能被点击
[MenuItem("GameObject/ My delete", true, 11)]static bool MyDeleteValidate(){if (Selection.gameObjects.Length>0){return true;}else{return false;}}[MenuItem("GameObject/ My delete", false, 11)]static void Test3(){Debug.Log("test3");}
另一种方法
属性也有一种
在Editor下创建EnemyChange 脚本 集成至ScriptabkeWizard
using UnityEditor;public class EnemyChange : ScriptableWizard {public int hp = 10; }
在Tools下新建方法
[MenuItem("Tools/CreateWuzard")]static void CreateWuzard(){ScriptableWizard.DisplayWizard<EnemyChange>("统一修改敌人");}
可以吧两个方法整合到一个方法里
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor;public class EnemyChange : ScriptableWizard {public int hp = 10;[MenuItem("Tools/CreateWuzard")]static void CreateWuzard(){ScriptableWizard.DisplayWizard<EnemyChange>("统一修改敌人","ChangeValue" );}//检测 Create按钮的点击public void OnWizardCreate(){GameObject[] games = Selection.gameObjects;foreach (GameObject item in games){item.GetComponent<PlayerHealth>().hp += hp;}Debug.Log("click");}}
选择多个一起修改变量
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor;public class EnemyChange : ScriptableWizard {public int hp = 10;[MenuItem("Tools/CreateWuzard")]static void CreateWuzard(){ScriptableWizard.DisplayWizard<EnemyChange>("统一修改敌人","ChangeValue" ,"Other Button");}//检测 Create按钮的点击public void OnWizardCreate(){GameObject[] games = Selection.gameObjects;foreach (GameObject item in games){PlayerHealth playerHealth = item.GetComponent<PlayerHealth>();Undo.RecordObject(playerHealth, "change"); //更改之前摁下快门 可以撤销 ctrl+zplayerHealth. hp += hp;}ShowNotification(new GUIContent(games.Length+ "个游戏物体的值被修改了"));Debug.Log("click");}void OnWizardOtherButton(){//这个按钮方法不会关闭 小窗口}void OnWizardUpdate()//当对话框弹出 和修改了字段的值的时候调用{errorString ="";helpString="";if(Selection.gameObjects.lenth>0){helpString+"您当前选择了"+Selection.gameObjects.Lenght+"个敌人";}else{errorString ="请至少选择一个敌人"; //errorString 信息报错Debug.Log("update");}void OnSelectionChange(){OnWizardUpdate();}}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31550368/viewspace-2200184/ ,如需转载,请注明出处,否则将追究法律责任。