菜单栏拓展(MenuItem
下面的方法一定要static修饰,不然编辑器无法显示MenuItem拓展
//18行true是是否开启当前"编辑器拓展/SetUIRoot"按钮可见,
//与下方"编辑器拓展/SetUIRoot"无关, 下面跟一个方法return返回判断
设置层级
序列化UiRoot里的变量
序列化赋值最后一个私有变量
关闭代码函数显示引用个数
//给组件右键菜单栏添加按钮
[MenuItem("CONTEXT/PlayerHealth/InitHealthAndSpeed")]//CONTEXT 组件名 按钮名 即可给组件添加右键按钮功能
static void InitHealthAndSpeed()
{
Debug.Log("InitHealthAndSpeed");
}
as 转型不管command.context是否是空都不会报错,空的话返回空,但是强转如果command.context为空会报错
static void InitHealthAndSpeed(MenuCommand command)//command 是当前正在操作的组件
{
Debug.Log(command.context.GetType().FullName);
CompleteProject.PlayerHealth health = command.context as CompleteProject.PlayerHealth;
health.startingHealth = 0;
health.flashSpeed = 0;
Debug.Log(command.context.name);//输出组件所在物体名字
}
在编辑器模式下不能使用GameObject.Destroy(item); 只能使用GameObject.DestroyImmediate(item);
[MenuItem("Tools/ShowInfo",false,1)]//CONTEXT 组件名 按钮名 即可给组件添加右键按钮功能
static void GetSelectionObj()
{
Debug.Log(Selection.activeObject);//获取鼠标选中的物体,如果多选返回最后选中的物体包含预制体
Debug.Log(Selection.activeGameObject);//获取Hieratchy面板选中的物体,如果多选返回最后选中的物体
foreach (var item in Selection.objects)
{
Debug.Log("PP"+item);//获取鼠标选中的所有物体
}
}
[MenuItem("Tools/DeleteObj", false, 1)]//CONTEXT 组件名 按钮名 即可给组件添加右键按钮功能
static void DeleteObjs()
{
foreach (GameObject item in Selection.objects)
{
GameObject.DestroyImmediate(item); //自己做的删除没法撤销删除
}
}
[MenuItem("Tools/DeleteObjsAndCanBack", false, 1)]//CONTEXT 组件名 按钮名 即可给组件添加右键按钮功能
//把删除操作注册到记录中
static void DeleteObjsAndCanBack()
{
foreach (GameObject item in Selection.objects)
{
//GameObject.DestroyImmediate(item);
Undo.DestroyObjectImmediate(item);//用这个方法删除可以撤销
}
}
[MenuItem("Tools/DeleteObjsKJJ _m", false, 0)]//自定义快捷键m
static void DeleteObjsKJJ()
{
foreach (GameObject item in Selection.objects)
{
//GameObject.DestroyImmediate(item);
Undo.DestroyObjectImmediate(item);//用这个方法删除可以撤销
}
}
//%=ctrl #=shift &=alt
[MenuItem("Tools/DeleteObjsKJJ2 %l", false, 0)]//自定义快捷键ctrl + l
static void DeleteObjsKJJ2()
{
foreach (GameObject item in Selection.objects)
{
//GameObject.DestroyImmediate(item);
Undo.DestroyObjectImmediate(item);//用这个方法删除可以撤销
}
}
[MenuItem("Tools/Tets1", true, 0)]//第二个参数是做验证的,只有达到某个条件,返回true,指定的按钮才会被开启
static bool Tets1Validate()
{
if (Selection.objects.Length > 0)
{
return true;
} else
{
return false;
}
}
[MenuItem("Tools/Tets1", false, 0)]//自定义快捷键ctrl + l
static void Tets1()
{
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/7db7958d1a8a43c0b9febcb3a2f71111.png
//第二种给组件添加菜单的方法,除系统组件外
[ContextMenu("SetColor")] //在目标类里面添加相应方法,赋予ContextMenu 特性
void SetColor()
{
flashColour = Color.green;
}
[ContextMenuItem("添加血量", "AddhP")] //给变量属性添加可以执行的方法 第一个参数按钮名字,第二个方法名
public int PlayerMoveSpeed;
void AddhP()
{
PlayerMoveSpeed += 200;
}
创建对话框
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class EnemyChange : ScriptableWizard
{
public int changeStartHealthValue = 10;
public int changeSinkSpeedValue = 1;
[MenuItem("Tools/CreateWizard")]
static void CreateWizard()
{
ScriptableWizard.DisplayWizard<EnemyChange>("统一修改敌人", "ChangeValueAndClose", "ChangeValue");//最多可以显示两个按钮
}
//第一个按钮的方法
private void OnWizardCreate()
{
//这个按扭点击了会关闭窗口
GameObject[] objects = Selection.gameObjects;
foreach (GameObject go in objects)
{
CompleteProject.EnemyHealth hp = go.GetComponent<CompleteProject.EnemyHealth>();
Undo.RecordObject(hp, "ChangeHp"); //要在值更改之前记录
hp.startingHealth += changeStartHealthValue;
hp.sinkSpeed += changeSinkSpeedValue;
}
ShowNotification(new GUIContent(Selection.gameObjects.Length + "个游戏物体被修改了")); //添加提示窗口
Debug.Log("ChangeValue");
}
//第二个按钮的方法
private void OnWizardOtherButton()
{
//这个按扭点击了不会关闭窗口
OnWizardCreate();
}
private void OnWizardUpdate()
{
errorString = null;
helpString = null;
if (Selection.gameObjects.Length > 0)
{
helpString = "你当前选择了" + Selection.gameObjects.Length + "个敌人";
}
else
{
errorString = "请至少选择一个敌人";
}
//Debug.Log("调用");//对话框弹出时调用一次,修改里面的值时调用一次
}
private void OnSelectionChange()
{
OnWizardUpdate();
}
}
ShowNotification(new GUIContent(Selection.gameObjects.Length + “个游戏物体被修改了”)); //添加提示窗口
使用编辑器专属的保存数据方法,以免窗口打开数据还原成默认值
EditorPrefs.SetInt(changeStartHealthValueKey, changeStartHealthValue);
EditorPrefs.SetInt(changeSinkSpeedValueKey, changeSinkSpeedValue);
窗口打开时读取数据
private void OnEnable()
{
changeStartHealthValue = EditorPrefs.GetInt(changeStartHealthValueKey, changeStartHealthValue);
changeSinkSpeedValue = EditorPrefs.GetInt(changeSinkSpeedValueKey, changeSinkSpeedValue);
}
最后的参数是刚开始并没有保存到本地时的默认值
显示进度提示框** EditorUtility.DisplayProgressBar**
private void OnWizardCreate()
{
//这个按扭点击了会关闭窗口
GameObject[] objects = Selection.gameObjects;
EditorUtility.DisplayProgressBar("进度","0/"+objects.Length+ "完成修改值1",0);
int count = 0;
foreach (GameObject go in objects)
{
CompleteProject.EnemyHealth hp = go.GetComponent<CompleteProject.EnemyHealth>();
Undo.RecordObject(hp, "ChangeHp"); //要在值更改之前记录
hp.startingHealth += changeStartHealthValue;
hp.sinkSpeed += changeSinkSpeedValue;
count++;
Thread.Sleep((int)(0.1 * 1000.0f));
Debug.Log("ChangeValue"+ count);
EditorUtility.DisplayProgressBar("进度", count+ "/" + objects.Length + "完成修改值2", (float)count/objects.Length);
}
EditorUtility.ClearProgressBar();
ShowNotification(new GUIContent(Selection.gameObjects.Length + "个游戏物体被修改了"));
Debug.Log("ChangeValue");
}
创建自己的窗体
using UnityEditor;
public class MyWindow : EditorWindow
{
[MenuItem("Tools/ ShowMyWindow")]
static void ShowMyWindow()
{
MyWindow window = EditorWindow.GetWindow<MyWindow>();
window.Show();
}
private string name = "";
private void OnGUI()
{
GUILayout.Label("这是我的窗口");
name = GUILayout.TextField(name);
if (GUILayout.Button("创建"))
{
GameObject go = new GameObject(name);
Undo.RegisterCreatedObjectUndo(go,"creatgameobject"); //记录创建方便撤销
}
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、