@@ -13,100 +13,147 @@ namespace Coffee.EditorExtensions
13
13
[ InjectablePropertyDrawer ( typeof ( UnityEventBase ) , true ) ]
14
14
public class UnityEventDrawerEx : UnityEventDrawer
15
15
{
16
- const BindingFlags bfAll = BindingFlags . NonPublic | BindingFlags . Public | BindingFlags . Instance | BindingFlags . Static | BindingFlags . FlattenHierarchy ;
17
- static readonly FieldInfo fiReorderableList = typeof ( UnityEventDrawer ) . GetField ( "m_ReorderableList" , bfAll ) ;
18
- static readonly FieldInfo fiCalls = typeof ( UnityEventBase ) . GetField ( "m_Calls" , bfAll ) ;
19
- static readonly FieldInfo fiRuntimeCalls = Type . GetType ( "UnityEngine.Events.InvokableCallList, UnityEngine" ) . GetField ( "m_RuntimeCalls" , bfAll ) ;
20
- static GUIStyle styleToggle ;
21
- static GUIStyle styleBg ;
22
-
23
- static bool foldStatus { get { return UnityEventDrawerExSettings . instance . foldStatus ; } set { UnityEventDrawerExSettings . instance . foldStatus = value ; } }
24
-
25
- static readonly Dictionary < Type , FieldInfo > fiDelegateMap = new Dictionary < Type , FieldInfo > ( ) ;
26
-
16
+ //################################
17
+ // Constant or Static Members.
18
+ //################################
19
+ const BindingFlags kBfAll = BindingFlags . NonPublic | BindingFlags . Public | BindingFlags . Instance | BindingFlags . Static | BindingFlags . FlattenHierarchy ;
20
+ static readonly FieldInfo s_FiReorderableList = typeof ( UnityEventDrawer ) . GetField ( "m_ReorderableList" , kBfAll ) ;
21
+ static readonly FieldInfo s_FiCalls = typeof ( UnityEventBase ) . GetField ( "m_Calls" , kBfAll ) ;
22
+ static readonly FieldInfo s_FiRuntimeCalls = Type . GetType ( "UnityEngine.Events.InvokableCallList, UnityEngine" ) . GetField ( "m_RuntimeCalls" , kBfAll ) ;
23
+
24
+
25
+
26
+ //################################
27
+ // Public Members.
28
+ //################################
29
+ /// <summary>
30
+ /// Gets the height of the property.
31
+ /// </summary>
32
+ /// <returns>The property height.</returns>
33
+ /// <param name="property">Property.</param>
34
+ /// <param name="label">Label.</param>
27
35
public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
28
36
{
29
- ReorderableList ro = fiReorderableList . GetValue ( this ) as ReorderableList ;
37
+ // Get the ReorderableList for default drawer.
38
+ ReorderableList ro = s_FiReorderableList . GetValue ( this ) as ReorderableList ;
30
39
if ( ro == null )
31
40
{
32
41
base . GetPropertyHeight ( property , label ) ;
33
- ro = fiReorderableList . GetValue ( this ) as ReorderableList ;
42
+ ro = s_FiReorderableList . GetValue ( this ) as ReorderableList ;
34
43
}
35
44
45
+ // If persistent calls is empty, display it compactry.
36
46
bool isEmpty = property . FindPropertyRelative ( "m_PersistentCalls" ) . FindPropertyRelative ( "m_Calls" ) . arraySize == 0 ;
37
47
ro . elementHeight = isEmpty
38
48
? 16
39
49
: 16 * 2 + 11 ;
40
50
41
- return foldStatus
51
+ // If drawer is folded, skip drawing runtime calls.
52
+ return s_FoldStatus
42
53
? base . GetPropertyHeight ( property , label ) + GetRuntimeCalls ( property ) . Count * 17
43
54
: base . GetPropertyHeight ( property , label ) ;
44
55
}
45
56
57
+ /// <summary>
58
+ /// Raises the GU event.
59
+ /// </summary>
60
+ /// <param name="position">Position.</param>
61
+ /// <param name="property">Property.</param>
62
+ /// <param name="label">Label.</param>
46
63
public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
47
64
{
65
+ // Draw background and toggle.
48
66
var RuntimeCalls = GetRuntimeCalls ( property ) ;
49
- float height = foldStatus
67
+ float height = s_FoldStatus
50
68
? RuntimeCalls . Count * 17 + 16
51
69
: 16 ;
52
70
var r = new Rect ( position . x + 2 , position . y + position . height - height , position . width - 4 , height ) ;
53
71
DrawRuntimeCallToggle ( r , RuntimeCalls . Count ) ;
54
72
73
+ // Draw UnityEvent using default drawer.
55
74
base . OnGUI ( position , property , label ) ;
56
75
57
- if ( foldStatus )
76
+ // If drawer is folded, skip drawing runtime calls.
77
+ if ( ! s_FoldStatus )
78
+ {
79
+ return ;
80
+ }
81
+
82
+ // Draw runtime calls.
83
+ r = new Rect ( r . x + 16 , r . y + 15 , r . width - 16 , 16 ) ;
84
+ EditorStyles . objectField . fontSize = 9 ;
85
+ foreach ( var invokableCall in RuntimeCalls )
58
86
{
59
- r = new Rect ( r . x + 16 , r . y + 15 , r . width - 16 , 16 ) ;
60
- EditorStyles . objectField . fontSize = 9 ;
61
- foreach ( var c in RuntimeCalls )
87
+ // Get delegate object from InvokableCall.
88
+ Type type = invokableCall . GetType ( ) ;
89
+ FieldInfo fiDelegate ;
90
+ if ( ! s_FiDelegateMap . TryGetValue ( type , out fiDelegate ) )
62
91
{
63
- DrawDelegate ( r , GetDelegate ( c ) ) ;
64
- r . y += r . height + 1 ;
92
+ fiDelegate = type . GetField ( "Delegate" , kBfAll ) ;
93
+ s_FiDelegateMap . Add ( type , fiDelegate ) ;
65
94
}
66
- EditorStyles . objectField . fontSize = 11 ;
95
+ Delegate del = fiDelegate . GetValue ( invokableCall ) as Delegate ;
96
+
97
+ // Draw delegate.
98
+ DrawDelegate ( r , del ) ;
99
+ r . y += r . height + 1 ;
67
100
}
101
+ EditorStyles . objectField . fontSize = 11 ;
68
102
}
69
103
70
- static Delegate GetDelegate ( object invokableCall )
71
- {
72
- Type type = invokableCall . GetType ( ) ;
73
- FieldInfo fiDelegate ;
74
- if ( ! fiDelegateMap . TryGetValue ( type , out fiDelegate ) )
75
- {
76
- fiDelegate = type . GetField ( "Delegate" , bfAll ) ;
77
- fiDelegateMap . Add ( type , fiDelegate ) ;
78
- }
79
104
80
- return fiDelegate . GetValue ( invokableCall ) as Delegate ;
81
- }
105
+ //################################
106
+ // Private Members.
107
+ //################################
108
+
109
+ static bool s_FoldStatus { get { return UnityEventDrawerExSettings . instance . foldStatus ; } set { UnityEventDrawerExSettings . instance . foldStatus = value ; } }
82
110
83
- static void DrawRuntimeCallToggle ( Rect rect , int count )
111
+ static readonly Dictionary < Type , FieldInfo > s_FiDelegateMap = new Dictionary < Type , FieldInfo > ( ) ;
112
+
113
+ static GUIStyle s_StyleToggle { get ; set ; }
114
+
115
+ static GUIStyle s_StyleBg { get ; set ; }
116
+
117
+ /// <summary>
118
+ /// Draws the runtime call toggle.
119
+ /// </summary>
120
+ /// <param name="position">Position.</param>
121
+ /// <param name="count">Runtime call count.</param>
122
+ static void DrawRuntimeCallToggle ( Rect position , int count )
84
123
{
85
- if ( styleBg == null )
124
+ // Cache style.
125
+ if ( s_StyleBg == null )
86
126
{
87
- styleBg = new GUIStyle ( "ProgressBarBack" ) ;
88
- styleToggle = new GUIStyle ( "OL Toggle" ) { fontSize = 9 } ;
89
- styleToggle . onNormal . textColor =
90
- styleToggle . normal . textColor =
91
- styleToggle . onActive . textColor =
92
- styleToggle . active . textColor = EditorStyles . label . normal . textColor ;
127
+ s_StyleBg = new GUIStyle ( "ProgressBarBack" ) ;
128
+ s_StyleToggle = new GUIStyle ( "OL Toggle" ) { fontSize = 9 } ;
129
+ s_StyleToggle . onNormal . textColor =
130
+ s_StyleToggle . normal . textColor =
131
+ s_StyleToggle . onActive . textColor =
132
+ s_StyleToggle . active . textColor = EditorStyles . label . normal . textColor ;
93
133
}
94
134
95
- GUI . Label ( rect , "" , styleBg ) ;
135
+ // Draw background.
136
+ GUI . Label ( position , "" , s_StyleBg ) ;
137
+
138
+ // Draw foldout with label.
96
139
string text = string . Format ( "Show runtime calls ({0})" , count ) ;
97
- rect . width -= 80 ;
98
- rect . height = 14 ;
99
- foldStatus = GUI . Toggle ( rect , foldStatus , text , styleToggle ) ;
140
+ s_FoldStatus = GUI . Toggle ( new Rect ( position . x , position . y , position . width - 80 , 14 ) , s_FoldStatus , text , s_StyleToggle ) ;
100
141
}
101
142
102
- static void DrawDelegate ( Rect rect , Delegate del )
143
+ /// <summary>
144
+ /// Draws the delegate.
145
+ /// </summary>
146
+ /// <param name="position">Position.</param>
147
+ /// <param name="del">Delegate.</param>
148
+ static void DrawDelegate ( Rect position , Delegate del )
103
149
{
104
- Rect r = new Rect ( rect . x , rect . y , rect . width * 0.3f , rect . height ) ;
105
150
try
106
151
{
152
+ Rect r = new Rect ( position . x , position . y , position . width * 0.3f , position . height ) ;
107
153
MethodInfo method = del . Method ;
108
154
object target = del . Target ;
109
155
156
+ // Draw the target if possible.
110
157
var obj = target as UnityEngine . Object ;
111
158
if ( obj )
112
159
{
@@ -121,28 +168,36 @@ static void DrawDelegate(Rect rect, Delegate del)
121
168
EditorGUI . LabelField ( r , "null" , EditorStyles . miniLabel ) ;
122
169
}
123
170
171
+ // Draw the method name.
124
172
r . x += r . width ;
125
- r . width = rect . width - r . width ;
173
+ r . width = position . width - r . width ;
126
174
EditorGUI . LabelField ( r , method . ReflectedType + "." + method . Name , EditorStyles . miniLabel ) ;
127
175
}
128
176
catch
129
177
{
130
- EditorGUI . LabelField ( rect , "null delegate" , EditorStyles . miniLabel ) ;
178
+ EditorGUI . LabelField ( position , "null delegate" , EditorStyles . miniLabel ) ;
131
179
}
132
180
}
133
181
182
+ /// <summary>
183
+ /// Gets the runtime call list from SerializedProperty.
184
+ /// </summary>
185
+ /// <returns>The runtime call list.</returns>
186
+ /// <param name="property">SerializedProperty.</param>
134
187
public static IList GetRuntimeCalls ( SerializedProperty property )
135
188
{
189
+ // Find FieldInfo for the runtime call list.
136
190
var instance = property . serializedObject . targetObject ;
137
191
Type type = instance . GetType ( ) ;
138
192
FieldInfo fiProperty = null ;
139
193
while ( type != null && fiProperty == null )
140
194
{
141
- fiProperty = type . GetField ( property . name , bfAll ) ;
195
+ fiProperty = type . GetField ( property . name , kBfAll ) ;
142
196
type = type . BaseType ;
143
197
}
144
198
145
- return fiRuntimeCalls . GetValue ( fiCalls . GetValue ( fiProperty . GetValue ( instance ) ) ) as IList ;
199
+ // Gets the runtime call list.
200
+ return s_FiRuntimeCalls . GetValue ( s_FiCalls . GetValue ( fiProperty . GetValue ( instance ) ) ) as IList ;
146
201
}
147
202
148
203
}
0 commit comments