Editor文件夹内的资源都不会被打包出去(包括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),] //快捷键为T
static 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+z
playerHealth. 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();
}
}
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删