正所谓:人靠衣装,佛靠金装。程序也如此,好的程序给人高潮。
例如下面的例子:
里面的菜单是系统自带的,但是如何能在里面留下我们的痕迹呢?
1、Editor(编辑器)
usingUnityEngine;
usingSystem.Collections;
usingUnityEditor; // 引用命名控件
publicclass EditorTest : Editor { // 继承Editor这个类
[MenuItem("SYTest/测试窗口",false)]
staticvoid STest()
{
Debug.Log("测试窗口");
}
// 快捷键是F1...F2 = F...
[MenuItem("SYTest1/测试窗口1 F2",false)]
staticvoid Test1()
{
Debug.Log("测试窗口1");
}
// 添加快捷键: % = Ctrl, # = Shift, & = Alt
// 如果是a-z,则前面写_a - _z这种带_前缀的
[MenuItem("SYTest2/测试窗口2 %_K",false)]
staticvoid Test2()
{
Debug.Log("测试窗口2");
}
}
1、多级菜单:
内部实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[MenuItem( "SYTest2/测试窗口1/我是谁 %_O" , false ,1)]
static void Test3()
{
Debug.Log( "我是谁" );
}
[MenuItem( "SYTest2/测试窗口1/我是1号 %_O" , false ,2)]
static void Test4()
{
Debug.Log( "我是1号" );
}
//后面的数字,代表优先级,数字越小,显示越靠前
[MenuItem( "SYTest2/测试窗口1/我是2号 %_O" , false ,3)]
static void Test5()
{
Debug.Log( "我是2号" );
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[MenuItem( "SYTest2/测试窗口1/我是2号 %_O" , true )]
static bool TestSy()
{
object obj = Selection.activeObject;
// 首先不等与空,并且obj的类型是GameObject类型
return obj != null && obj.GetType() == typeof (GameObject);
}
[MenuItem( "SYTest2/测试窗口1/我是2号 %_O" )]
static void Test6()
{
Debug.Log( "我是2号" );
}
|
2.Inspector面板
我们添加系统组件的时候,发现面板很好看,而脚本做不到这种效果。
利用GUILayout 绘制UI控件
3.窗口的菜单
填充更多内容
4.如何为Assets添加额外扩展
总结:
顶部编辑器菜单:引用using UnityEditor; 使用[MenuItem()];
Inspector面板的处理:继承Editor,重写虚方法:OnInspectorGUI(),并调用(父类的此方法)base.OnInspectorGUI()
窗口的面板:继承:EditorWindow, 使用EditorWindow.GetWindow(); 调用窗口的Show()方法。