Unity编辑器扩展
using UnityEditor; //作为编辑器时的引用
1、添加菜单,需要静态方法
[MenuItem("Tools/Test")]
static void Test()
{
}
2、菜单的分类
[MenuItem("Tools/Test1", true, 1)] (2)是否验证
static bool Test1()
{
true //可以使用
false //不可使用
}
[MenuItem("Tools/Test1", false, 1)] (3)优先级,默认为1000,相差11可以加一条线进行分类
static void Test1()
{
}
3、组件增加右键菜单(脚本)
[MenuItem("CONTEXT/PlayerHealth/Init")] //1-固定 2-脚本名 3-按键名
static void Test(MenuCommand cmd) //MenuCommand是当前正在操作的组件
{
PlayerHealth health = (PlayerHealth) cmd.context;//得到脚本
}
[ContextMenuItem("按键名", "执行方法")]
public int index = 0;
[ContextMenu("名字")] //可编辑脚本使用
void SetColor()
{
}
4、可撤销操作
Undo类,使用Undo操作的选中物体,可以进入撤销
5、增加快捷键
[MenuItem("Tools/test2 _t")] // %=ctrl #=shift &=alt
6、对话框
public class Tools : ScriptableWizard
{
[MenuItem("Tools/Win")]
static void Creat()
{
ScriptableWizard.DisplayWizard<Tools>("哈哈哈", "Change Value", "第二个按键的名字");
}
public int hp = 100;
public string name = "哈";
void OnWizardCreate() //监听create按键的点击
{
Undo.RecordObject(hp,"hp");//记录修改前的,可以用来撤销
Debug.Log("11111");
}
void OnWizardOtherButton()//监听 第二个按键的名字
{
}
ShowNotification(new GUIContent("1111111"));//弹框
//窗口弹出、值修改调用
void OnWizardUpdate()
{
helpString = "帮助信息";
errorString = "错误信息";
}
//选择物体数量改变调用
void OnSelectionChange()
{
}
}
8、显视进度条
EditorUtility类 工具类
EditorUtility.DisplayProgressBar("","",0.1f); //标题 提示信息 当前进度
EditorUtility.ClearProgressBar(); //隐藏进度条
EditorUtility.DisplayCancelableProgressBar("", "", 0.1f); //可以取消的进度条
9、自定义窗口
public class MyWindow : EditorWindow
{
[MenuItem("Window/Show MyWindow")]
static void ShowMyWindow()
{
MyWindow window = EditorWindow.GetWindow<MyWindow>(); //得到这个窗口类
window.Show();
}
void OnGUI() //使用OnGUI可以在自定义窗口中绘制
{
GUILayout.Label(""); //绘制类
EditorGUILayout
Undo.RegisterCompleteObjectUndo(); //记录操作 1-被操作的物体 2-名字,操作记录名 主要做撤销
}
}