|
在使用Unity3D来开发大型的项目中,我们通常会自己开发一些适合自己的编辑器来便于一些非程序人员对游戏内容进行编辑。Unity3D给我们开放了这样的一些接口:
一
导航条
加入我们需要在导航条中增加一些我们自己的小工具。我们可以如下使用:
|
01
|
usingUnityEditor;
|
|
02
|
|
|
03
|
staticpublicclassWayPointTool
|
|
04
|
|
|
05
|
{
|
|
06
|
|
|
07
|
privatestaticintindex
=0;
|
|
08
|
|
|
09
|
privatestaticDictionary<string,NvWayPoint>m_WayPointJasonTable;
|
|
10
|
|
|
11
|
privatestaticDictionary<int,WayPoint>m_WayPointTable;
|
|
12
|
|
|
13
|
[MenuItem(“Game Tool Chain/WayPoint Tool/Reload WayPoint”)]
|
|
14
|
|
|
15
|
staticvoidReloadWayPoint()
|
|
16
|
|
|
17
|
{
|
|
18
|
|
|
19
|
}
|
|
20
|
|
|
21
|
}
|
因为在导航条中的工具类没有被编辑器或者使用者实例化,所以,类和对象以及函数都需要是静态的。
二
编辑器窗口
我们也可以在编辑器中添加一些我们想要的窗口,所创建的窗口会像3.5中的Navigation窗口一样。具体窗口中的内容需要定义的话,要使用Unity3D提供的类:EditorWindow。此类窗口的特点是没有对象的概念,是全局使用的窗口类。
|
01
|
usingUnityEngine;
|
|
02
|
|
|
03
|
usingUnityEditor;
|
|
04
|
|
|
05
|
publicclassMyWindow
:EditorWindow
|
|
06
|
|
|
07
|
{
|
|
08
|
|
|
09
|
// Add menu named “My Window” to the Window menu
|
|
10
|
|
|
11
|
[MenuItem
(“Window/My Window”)]
|
|
12
|
|
|
13
|
staticvoidInit
(){
|
|
14
|
|
|
15
|
// Get existing open window or if none, make a new one:
|
|
16
|
|
|
17
|
MyWindow window =(MyWindow)EditorWindow.GetWindow(typeof(MyWindow));
|
|
18
|
|
|
19
|
voidOnGUI
()
|
|
20
|
|
|
21
|
{
|
|
22
|
|
|
23
|
GUILayout.Label(“BaseSettings”,
EditorStyles.boldLabel);myString
|
|
24
|
|
|
25
|
EditorGUILayout.TextField(“Text Field”, myString);groupEnabled
|
|
26
|
|
|
27
|
EditorGUILayout.BeginToggleGroup(“Optional Settings”, groupEnabled);
|
|
28
|
|
|
29
|
myBool =EditorGUILayout.Toggle(“Toggle”, myBool);
|
|
30
|
|
|
31
|
myFloat =EditorGUILayout.Slider(“Slider”, myFloat,
-3,
3);
|
|
32
|
|
|
33
|
EditorGUILayout.EndToggleGroup();
|
|
34
|
|
|
35
|
EditorGUIUtility.LookLikeInspector();
|
|
36
|
|
|
37
|
EditorGUILayout.TextField(“Text Field:”, “Hello
There”);
|
|
38
|
|
|
39
|
EditorGUILayout.IntField(“IntField:”,
integer1);
|
|
40
|
|
|
41
|
EditorGUILayout.FloatField(“FloatField:”,
float1);
|
|
42
|
|
|
43
|
EditorGUILayout.Space();
|
|
44
|
|
|
45
|
EditorGUIUtility.LookLikeControls();
|
|
46
|
|
|
47
|
EditorGUILayout.TextField(“Text Field”, “Hello There”);
|
|
48
|
|
|
49
|
EditorGUILayout.IntField(“IntField:”,
integer1);
|
|
50
|
|
|
51
|
EditorGUILayout.FloatField(“FloatField:”,
float1);
|
|
52
|
|
|
53
|
}
|
|
54
|
|
|
55
|
}
|
三
自定义对象的编辑窗口
你可能需要对自己定义的类对象暴露一些接口给使用编辑器的开发人员。比如你想让他编辑一个UI控件之类的对象,你可以使用下面的方法来实现:
|
01
|
usingUnityEngine;
|
|
02
|
|
|
03
|
usingUnityEditor;
|
|
04
|
|
|
05
|
[CustomEditor(typeof(UIWidget))]//
对UIWidget
对象编辑窗进行的重构
|
|
06
|
|
|
07
|
publicclassUIWidgetInspector
:Editor
|
|
08
|
|
|
09
|
{
|
|
10
|
|
|
11
|
protectedUIWidget mWidget;
|
|
12
|
|
|
13
|
staticprotectedboolmUseShader
=false;
|
|
14
|
|
|
15
|
boolmInitialized
=false;
|
|
16
|
|
|
17
|
protectedboolmAllowPreview
=true;
|
|
18
|
|
|
19
|
publicoverridevoidOnInspectorGUI
()
|
|
20
|
|
|
21
|
{
|
|
22
|
|
|
23
|
}
|
|
24
|
|
|
25
|
}
|
|