【unity】拓展自定義編輯器窗口(二)

http://www.xuanyusong.com/archives/2211編輯器

Unity支持自行建立窗口,也支持自定義窗口布局。在Project視圖中建立一個Editor文件夾,在文件夾中在建立一條腳本。ide

自定義窗口須要讓腳本繼承EditorWindow在設置MenuItem,此時在Unity導航菜單欄中GameObjec->window就可建立一個自定義窗口。佈局

 

C#this

1spa

2code

3orm

4對象

5blog

6繼承

7

8

9

10

11

12

13

14

15

using UnityEngine;

using UnityEditor;

public class MyEditor : EditorWindow

{

 

    [MenuItem ("GameObject/window")]

    static void AddWindow ()

{      

//建立窗口

Rect  wr = new Rect (0,0,500,500);

        MyEditor window = (MyEditor)EditorWindow.GetWindowWithRect (typeof (MyEditor),wr,true,"widow name");

window.Show();

 

    }

}

 EditorWindow.GetWindowWithRect() 和 EditorWindow.GetWindow()均可以建立一個窗口。前者能夠規定窗口的區域,後者可經過鼠標動態的延伸窗口。參數1表示窗口的對象,參數2表示窗口的區域,參數3表示窗口類型true表示窗口不會被別的窗口覆蓋,參數4表示窗口的名稱。

wKiom1WksSfjmb5-AAEo3vb_a80556.jpg

 

而後咱們在擴充一下自定義窗口,仔細看看窗口的生命週期。

 

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

using UnityEngine;

using UnityEditor;

public class MyEditor : EditorWindow

{

 

    [MenuItem ("GameObject/window")]

    static void AddWindow ()

{      

//建立窗口

Rect  wr = new Rect (0,0,500,500);

        MyEditor window = (MyEditor)EditorWindow.GetWindowWithRect (typeof (MyEditor),wr,true,"widow name");

window.Show();

 

    }

 

//輸入文字的內容

private string text;

//選擇貼圖的對象

private Texture texture;

 

public void Awake ()

{

//在資源中讀取一張貼圖

texture = Resources.Load("1") as Texture;

}

 

//繪製窗口時調用

    void OnGUI ()

{

//輸入框控件

text = EditorGUILayout.TextField("輸入文字:",text);

 

if(GUILayout.Button("打開通知",GUILayout.Width(200)))

{

//打開一個通知欄

this.ShowNotification(new GUIContent("This is a Notification"));

}

 

if(GUILayout.Button("關閉通知",GUILayout.Width(200)))

{

//關閉通知欄

this.RemoveNotification();

}

 

//文本框顯示鼠標在窗口的位置

EditorGUILayout.LabelField ("鼠標在窗口的位置", Event.current.mousePosition.ToString ());

 

//選擇貼圖

texture =  EditorGUILayout.ObjectField("添加貼圖",texture,typeof(Texture),true) as Texture;

 

if(GUILayout.Button("關閉窗口",GUILayout.Width(200)))

{

//關閉窗口

this.Close();

}

 

    }

 

//更新

void Update()

{

 

}

 

void OnFocus()

{

Debug.Log("當窗口得到焦點時調用一次");

}

 

void OnLostFocus()

{

Debug.Log("當窗口丟失焦點時調用一次");

}

 

void OnHierarchyChange()

{

Debug.Log("當Hierarchy視圖中的任何對象發生改變時調用一次");

}

 

void OnProjectChange()

{

Debug.Log("當Project視圖中的資源發生改變時調用一次");

}

 

void OnInspectorUpdate()

{

   //Debug.Log("窗口面板的更新");

   //這裏開啓窗口的重繪,否則窗口信息不會刷新

   this.Repaint();

}

 

void OnSelectionChange()

{

//當窗口出去開啓狀態,而且在Hierarchy視圖中選擇某遊戲對象時調用

foreach(Transform t in Selection.transforms)

{

//有多是多選,這裏開啓一個循環打印選中游戲對象的名稱

Debug.Log("OnSelectionChange" + t.name);

}

}

 

void OnDestroy()

{

Debug.Log("當窗口關閉時調用");

}

}

 

wKioL1WkswLzGEViAAC-DqwnS4c946.jpg

 

下載內容:http://vdisk.weibo.com/s/xHLuJ

相關文章
相關標籤/搜索