Unity3D教程:编辑器编程

在使用Unity3D来开发大型的项目中,我们通常会自己开发一些适合自己的编辑器来便于一些非程序人员对游戏内容进行编辑。Unity3D给我们开放了这样的一些接口:

导航条

加入我们需要在导航条中增加一些我们自己的小工具。我们可以如下使用:


01

  using UnityEditor;

02

 

03

  static public class WayPointTool

04

 

05

  {

06

 

07

  private static int index = 0 ;

08

 

09

  private static Dictionary<string,NvWayPoint> m_WayPointJasonTable;

10

 

11

  private static Dictionary<int,WayPoint> m_WayPointTable;

12

 

13

  [MenuItem(“Game Tool Chain/WayPoint Tool/Reload WayPoint”)]

14

 

15

  static void ReloadWayPoint()

16

 

17

  {

18

 

19

  }

20

 

21

  }

 

因为在导航条中的工具类没有被编辑器或者使用者实例化,所以,类和对象以及函数都需要是静态的。

编辑器窗口

我们也可以在编辑器中添加一些我们想要的窗口,所创建的窗口会像3.5中的Navigation窗口一样。具体窗口中的内容需要定义的话,要使用Unity3D提供的类:EditorWindow。此类窗口的特点是没有对象的概念,是全局使用的窗口类。


01

  using UnityEngine;

02

 

03

  using UnityEditor;

04

 

05

  public class MyWindow : EditorWindow

06

 

07

  {

08

 

09

  // Add menu named “My Window” to the Window menu

10

 

11

  [MenuItem (“Window/My Window”)]

12

 

13

  static void Init () {

14

 

15

  // Get existing open window or if none, make a new one:

16

 

17

  MyWindow window = (MyWindow)EditorWindow.GetWindow (typeof (MyWindow));

18

 

19

  void OnGUI ()

20

 

21

  {

22

 

23

  GUILayout.Label (Base Settings”, 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(Int Field:”, integer1);

40

 

41

  EditorGUILayout.FloatField(Float Field:”, float1);

42

 

43

  EditorGUILayout.Space();

44

 

45

  EditorGUIUtility.LookLikeControls();

46

 

47

  EditorGUILayout.TextField (“Text Field”, “Hello There”);

48

 

49

  EditorGUILayout.IntField(Int Field:”, integer1);

50

 

51

  EditorGUILayout.FloatField(Float Field:”, float1);

52

 

53

  }

54

 

55

  }

 

自定义对象的编辑窗口

你可能需要对自己定义的类对象暴露一些接口给使用编辑器的开发人员。比如你想让他编辑一个UI控件之类的对象,你可以使用下面的方法来实现:


01

using UnityEngine;

02

 

03

  using UnityEditor;

04

 

05

  [CustomEditor(typeof(UIWidget))]  // UIWidget 对象编辑窗进行的重构

06

 

07

  public class UIWidgetInspector : Editor

08

 

09

  {

10

 

11

  protected UIWidget mWidget;

12

 

13

  static protected bool mUseShader = false;

14

 

15

  bool mInitialized = false;

16

 

17

  protected bool mAllowPreview = true;

18

 

19

  public override void OnInspectorGUI ()

20

 

21

  {

22

 

23

  }

24

 

25

  }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值