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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using UnityEditor;
namespace TweenAnimation
{
[CustomEditor(typeof(TweenAnimation), false)]
public partial class TweenAnimationInspector : Editor
{
TweenModuleGUIStyles styles
{
get
{
return TweenModuleGUIStyles.Get();
}
}
TweenModuleUI ui
{
get
{
return TweenModuleUI.Get();
}
}
float value = 20;
TweenAnimation animation;
bool m_ShowAnimationTab = true;
static Dictionary<string, Type> s_TweenModuleClasses;
static string[] s_TweenModuleClassNames;
static Dictionary<string, MethodInfo> s_TweenModuleGUI;
public void OnEnable()
{
m_ShowAnimationTab = true;
s_TweenModuleGUI = new Dictionary<string, MethodInfo>();
}
public override void OnInspectorGUI()
{
animation = target as TweenAnimation;
if (animation == null)
return;
EditorGUILayout.Space();
AnimationTab();
AddNewModule();
AnimationModules();
EditorGUILayout.Space();
}
void AnimationTab()
{
Rect rect = ui.GetControlRect(40);
Rect headerRect = rect;
headerRect.x -= 2;
headerRect.width += 8;
if (GUI.Button(headerRect, "", styles.headerBg))
{
m_ShowAnimationTab = !m_ShowAnimationTab;
}
GUI.Label(new Rect(rect.x + 10, rect.y + 3, 100, 20), "Tween Animation", styles.headerTitle);
Vector2 size = styles.text.CalcSize(new GUIContent(animation.description));
GUI.Label(new Rect(rect.x + rect.width - size.x - 10, rect.y + 3, 100, 20), animation.description, styles.text);
if (m_ShowAnimationTab)
{
animation.description = ui.GUIText("Description", animation.description);
// 播放风格
animation.playbackStyle = (TweenAnimation.PlaybackStyle) ui.GUIEnum("Playback Style", animation.playbackStyle);
if(animation.playbackStyle != TweenAnimation.PlaybackStyle.Once)
{
// 播放次数限制
string fmt = animation.playbackLimit <= 0 ? "Unlimited" : "";
float limit = ui.GUIFloat("Playback Limit", animation.playbackLimit, fmt);
limit = Mathf.Clamp(limit, 0, Int32.MaxValue);
animation.playbackLimit = animation.playbackLimit > limit ? (int)Mathf.Floor(limit) : (int)Mathf.Ceil(limit);
}
// 事件触发方向
animation.eventTriggeredDirection = (TweenAnimation.EventTriggeredDirection)ui.GUIEnumMask("Event Direction", animation.eventTriggeredDirection);
}
DrawBgWire(rect, (m_ShowAnimationTab ? 4 : -2));
if(!m_ShowAnimationTab)
GUILayout.Space(-4);
else
GUILayout.Space(2);
}
void AddNewModule()
{
Rect rect = ui.GetControlRect(20);
Rect labelRect = rect;
labelRect.y += 2;
GUI.Label(labelRect, "Modules ", styles.text);
rect.x = rect.x + rect.width - 15;
rect.width = 20;
rect.height = 20;
//if (GUI.Button(rect, "", styles.plus))
{
if(s_TweenModuleClasses == null)
{
s_TweenModuleClasses = new Dictionary<string, Type>();
// 获得TweenModule的派生类
var classes = Assembly
.GetAssembly(typeof(TweenModule))
.GetTypes()
.Where(t => t.IsSubclassOf(typeof(TweenModule)));
foreach(var itor in classes)
{
string name = itor.Name;
Type type = itor;
s_TweenModuleClasses.Add(name, type);
}
s_TweenModuleClassNames = null;
}
if(s_TweenModuleClassNames == null)
{
s_TweenModuleClassNames = new string[s_TweenModuleClasses.Count];
int i = 0;
var itor = s_TweenModuleClasses.GetEnumerator();
while(itor.MoveNext())
{
s_TweenModuleClassNames[i++] = itor.Current.Key;
}
}
int selected = EditorGUI.Popup(rect, -1, s_TweenModuleClassNames, styles.plus);
if(selected >= 0 && selected < s_TweenModuleClassNames.Length)
{
string name = s_TweenModuleClassNames[selected];
Type type = s_TweenModuleClasses[name];
var module = Activator.CreateInstance(type);
animation.AddModule(module as TweenModule);
}
}
GUILayout.Space(-4);
}
void AnimationModules()
{
if (animation.modules == null)
return;
for(int i = 0; i < animation.modules.Count; ++i)
{
TweenModule module = animation.modules[i];
AnimaitonModule(module, i == animation.modules.Count - 1);
}
}
void AnimaitonModule(TweenModule module, bool isLast)
{
string name = module.name;
string description = module.description;
bool enabled = module.enabled;
// header
Rect rect = ui.GetControlRect(20);
if (Event.current.type == EventType.MouseDown || Event.current.type == EventType.MouseUp)
{
Rect checkRect = rect;
checkRect.x += 1;
checkRect.y += 1;
checkRect.width = 20;
checkRect.height = 20;
module.enabled = GUI.Toggle(checkRect, enabled, "", styles.checkmark);
Rect headerRect = rect;
headerRect.x -= 2;
headerRect.width += 8;
if (GUI.Button(headerRect, "", styles.headerBg))
module.unfold = !module.unfold;
}
else if(Event.current.type == EventType.Repaint || Event.current.type == EventType.Layout)
{
Rect headerRect = rect;
headerRect.x -= 2;
headerRect.width += 8;
GUI.Button(headerRect, "", styles.headerBg);
Rect checkRect = rect;
checkRect.x += 1;
checkRect.y += 1;
checkRect.width = 20;
checkRect.height = 20;
GUI.Toggle(checkRect, enabled, "", styles.checkmark);
}
Vector2 size = styles.text.CalcSize(new GUIContent(name));
GUI.Label(new Rect(rect.x + 15, rect.y + 2, size.x, 20), name, styles.text);
// content
if (module.unfold)
{
MethodInfo method;
string classname = module.GetType().Name;
if (!s_TweenModuleGUI.TryGetValue(classname, out method))
{
Type type = this.GetType();
method = type.GetMethod("OnInspector_" + classname);
s_TweenModuleGUI.Add(classname, method);
}
if (method != null)
{
object[] parameter = new object[]{ module};
method.Invoke(this, parameter);
}
else
{
Debug.LogError("没有对应的绘制函数" + "OnInspector_" + classname);
}
}
// wire
DrawBgWire(rect, module.unfold ? 4 : -2);
if (!module.unfold)
GUILayout.Space(-4);
else
GUILayout.Space(2);
if (!isLast)
GUILayout.Space(-1);
}
void DrawBgWire(Rect firstRect, int hOff)
{
Rect bgRect = ui.GetLastControlRect();
bgRect.height = bgRect.y + bgRect.height - firstRect.y + hOff/*(m_ShowAnimationTab ? 4 : -2)*/;
bgRect.x = firstRect.x - 3;
bgRect.y = firstRect.y - 1;
bgRect.width = firstRect.width + 5;
GUI.Label(bgRect, "", styles.bgSmall);
}
}
}
|