Editor

思考并回答以下问题:

OnSceneGUI

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
using UnityEngine;
using System.Collections;
using UnityEditor;

// 自定义脚本挂在哪个物体上面,我们就对哪个物体进行操作
[CustomEditor(typeof(ChangeObjInfor))]
public class SceneEditor : Editor
{
private void OnSceneGUI()
{
// 得到脚本所挂载的物体,目标
ChangeObjInfor theTarget = (ChangeObjInfor)target;
Handles.Label(theTarget.transform.position+Vector3.up*2,theTarget.name+":"+theTarget.transform.position.ToString());
// 开始绘制GUI
Handles.BeginGUI();
// 规定GUI的显示区域
GUILayout.BeginArea(new Rect(0,0,150,200));
// 绘制文本提示
GUILayout.Label("选择颜色");

GUI.color = Color.red;
if (GUILayout.Button("红色"))
{
// Debug.Log("红色");
theTarget.GetComponent<Renderer>().sharedMaterial.color = Color.red;
}
GUI.color = Color.yellow;
if (GUILayout.Button("黄色"))
{
// Debug.Log("黄色");
theTarget.GetComponent<Renderer>().sharedMaterial.color = Color.yellow;
}
GUI.color = Color.green;
if (GUILayout.Button("绿色"))
{
// Debug.Log("绿色");
theTarget.GetComponent<Renderer>().sharedMaterial.color = Color.green;
}
GUILayout.EndArea();
Handles.EndGUI();
}
}
1
2
3
4
5
6
7
using UnityEngine;
using System.Collections;

public class ChangeObjInfor : MonoBehaviour
{

}

OnInspectorGUI

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
using UnityEngine;
using System.Collections;
using UnityEditor;
using Tool;

[CustomEditor(typeof(ChangeInspector))]
public class InspectorEditor : Editor
{
bool groundEnable = false;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
ChangeInspector inspectorObj = (ChangeInspector)target;

// 绘制贴图槽
inspectorObj.Texture = EditorGUILayout.ObjectField("选择贴图",inspectorObj.Texture,typeof(Texture),true)as Texture;
inspectorObj.RectValue = EditorGUILayout.RectField("窗口坐标",inspectorObj.RectValue);
inspectorObj.Remark = EditorGUILayout.TextField("备注",inspectorObj.Remark);

// 绘制滑动条
inspectorObj.SliderValue = EditorGUILayout.Slider("进度值",inspectorObj.SliderValue,0f,1f);
inspectorObj.IsOpen = EditorGUILayout.Toggle("开启",inspectorObj.IsOpen);
inspectorObj.TheType = (EDirType)EditorGUILayout.EnumPopup("方向",inspectorObj.TheType);

GUILayout.Label("*****以下是附加设置*****");
groundEnable = EditorGUILayout.BeginToggleGroup("是否开启附加设置", groundEnable);
inspectorObj.TheValue1 = EditorGUILayout.FloatField("值1",inspectorObj.TheValue1);
inspectorObj.TheValue2 = EditorGUILayout.FloatField("值2", inspectorObj.TheValue2);
inspectorObj.IsAdd = EditorGUILayout.Toggle("开启", inspectorObj.IsAdd);
EditorGUILayout.EndToggleGroup();
}

void OnEnable()
{

}

void OnDestroy()
{

}
}
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using UnityEngine;
using System.Collections;
using Tool;

public class ChangeInspector : MonoBehaviour {

private Texture texture;
private Rect rectValue;
private string remark;//备注
private float sliderValue = 0.8f;//进度值
private bool isOpen = true;
private EDirType theType = EDirType.up;

private float theValue1 = 0;
private float theValue2 = 0;
private bool isAdd = false;

public Texture Texture
{
get
{
return texture;
}

set
{
texture = value;
}
}

public Rect RectValue
{
get
{
return rectValue;
}

set
{
rectValue = value;
}
}

public string Remark
{
get
{
return remark;
}

set
{
remark = value;
}
}

public float SliderValue
{
get
{
return sliderValue;
}

set
{
sliderValue = value;
}
}

public bool IsOpen
{
get
{
return isOpen;
}

set
{
isOpen = value;
}
}

public EDirType TheType
{
get
{
return theType;
}

set
{
theType = value;
}
}

public float TheValue1
{
get
{
return theValue1;
}

set
{
theValue1 = value;
}
}

public float TheValue2
{
get
{
return theValue2;
}

set
{
theValue2 = value;
}
}

public bool IsAdd
{
get
{
return isAdd;
}

set
{
isAdd = value;
}
}
}

Window

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
using UnityEngine;
using System.Collections;
using UnityEditor;

public class MyWindowEditor : EditorWindow {
bool groundEnable = false;
float value1 = 0;
float value2 = 0;
[MenuItem("GameObject/ShowWindow")]
static void ShowWindow()
{
Rect theRect = new Rect(0,0,500,500);
MyWindowEditor window =(MyWindowEditor) EditorWindow.GetWindowWithRect(typeof(MyWindowEditor),theRect,true,"我的窗口");
window.Show();
}
void OnGUI()
{
if (GUILayout.Button("打开通知",GUILayout.Width(220)))
{
this.ShowNotification(new GUIContent("通知的内容"));
}
if (GUILayout.Button("关闭通知", GUILayout.Width(220)))
{
this.RemoveNotification();
}
if (GUILayout.Button("关闭窗口", GUILayout.Width(220)))
{
this.Close();
}
GUILayout.Label("以下是基础设置");
groundEnable = EditorGUILayout.BeginToggleGroup("启用",groundEnable);
value1 = EditorGUILayout.FloatField("值1",value1);
value2 = EditorGUILayout.FloatField("值1", value2);
EditorGUILayout.EndFadeGroup();
}
//窗口获得焦点的时候被调用
void OnFocus()
{
Debug.Log("窗口获得了焦点");
}
//窗口失去焦点的时候调用
void OnLostFocus()
{
Debug.Log("窗口失去了焦点");
}
void OnHierarchyChange()
{
Debug.Log("层次面板的对象发生改变");
}
void OnProjectChange()
{
Debug.Log("项目面板的对象发生改变");
}
void OnSelectionChange()
{
foreach (Transform item in Selection.transforms)
{
Debug.Log("被选中的物体是"+item.name);
}
}
private void OnDestroy()
{
Debug.Log("窗口被关闭了");
}
}

Camera

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(Camera))]
public class CameraEditor : Editor {

public override void OnInspectorGUI()
{
base.DrawDefaultInspector();
GUILayout.Label("********以下是拓展选项*********");
if (GUILayout.Button("点击处理"))
{
Debug.Log("检测到点击了");
}
}
}

Light

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(Light))]
public class LightEditor : Editor {
public override void OnInspectorGUI()
{
base.DrawDefaultInspector();
if (GUILayout.Button("按钮"))
{
Debug.Log("点击按钮");
}
}

}
0%