@@ -19,7 +19,7 @@ public class ScriptableObjectMultiSelectionDropdownDrawer : PropertyDrawer
19
19
private static List < ScriptableObject > _scriptableObjects = new List < ScriptableObject > ( ) ;
20
20
private static List < ScriptableObject > _selectedScriptableObjects = new List < ScriptableObject > ( ) ;
21
21
private static readonly int _controlHint = typeof ( ScriptableObjectMultiSelectDropdownAttribute ) . GetHashCode ( ) ;
22
- private static GUIContent _tempContent = new GUIContent ( ) ;
22
+ private static GUIContent _popupContent = new GUIContent ( ) ;
23
23
private static int _selectionControlID ;
24
24
private static readonly GenericMenu . MenuFunction2 _onSelectedScriptableObject = OnSelectedScriptableObject ;
25
25
private static bool isChanged ;
@@ -46,6 +46,9 @@ public override float GetPropertyHeight(SerializedProperty property, GUIContent
46
46
return EditorStyles . popup . CalcHeight ( GUIContent . none , 0 ) ;
47
47
}
48
48
49
+ /// <summary>
50
+ /// How you can get type of field which it uses PropertyAttribute
51
+ /// </summary>
49
52
private static Type GetPropertyType ( SerializedProperty property )
50
53
{
51
54
Type parentType = property . serializedObject . targetObject . GetType ( ) ;
@@ -71,11 +74,17 @@ private static bool ValidateProperty(SerializedProperty property)
71
74
return true ;
72
75
}
73
76
77
+ /// <summary>
78
+ /// When new ScriptableObject added to the project
79
+ /// </summary>
74
80
private static void ClearCache ( )
75
81
{
76
82
_scriptableObjects . Clear ( ) ;
77
83
}
78
84
85
+ /// <summary>
86
+ /// Gets ScriptableObjects just when it is a first time or new ScriptableObject added to the project
87
+ /// </summary>
79
88
private static void GetScriptableObjects ( ScriptableObjectMultiSelectDropdownAttribute attribute )
80
89
{
81
90
UnityEngine . Object [ ] loadedObject = Resources . LoadAll ( "" , attribute . BaseType ) ;
@@ -85,6 +94,9 @@ private static void GetScriptableObjects(ScriptableObjectMultiSelectDropdownAttr
85
94
}
86
95
}
87
96
97
+ /// <summary>
98
+ /// Checks if the ScriptableObject is selected or not by checking if the list contains it.
99
+ /// </summary>
88
100
private static bool ResolveSelectedScriptableObject ( ScriptableObject scriptableObject )
89
101
{
90
102
if ( _selectedScriptableObjects == null )
@@ -117,6 +129,9 @@ private static void Draw(Rect position, GUIContent label,
117
129
}
118
130
}
119
131
132
+ /// <summary>
133
+ /// Iterats through the property for finding selected ScriptableObjects
134
+ /// </summary>
120
135
private static ScriptableObject [ ] Read ( SerializedProperty property )
121
136
{
122
137
List < ScriptableObject > selectedScriptableObjects = new List < ScriptableObject > ( ) ;
@@ -133,10 +148,14 @@ private static ScriptableObject[] Read(SerializedProperty property)
133
148
return selectedScriptableObjects . ToArray ( ) ;
134
149
}
135
150
151
+ /// <summary>
152
+ /// Iterats through the property for storing selected ScriptableObjects
153
+ /// </summary>
136
154
private static void Write ( SerializedProperty property , ScriptableObject [ ] scriptableObjects )
137
155
{
138
- //var w = new System.Diagnostics.Stopwatch();
139
- //w.Start();
156
+ // Faster way
157
+ // var w = new System.Diagnostics.Stopwatch();
158
+ // w.Start();
140
159
int i = 0 ;
141
160
SerializedProperty iterator = property . Copy ( ) ;
142
161
iterator . arraySize = scriptableObjects . Length ;
@@ -149,9 +168,9 @@ private static void Write(SerializedProperty property, ScriptableObject[] script
149
168
i ++ ;
150
169
}
151
170
}
152
- //w.Stop();
153
- //long milliseconds = w.ElapsedMilliseconds;
154
- //Debug.Log(w.Elapsed.TotalMilliseconds + " ms");
171
+ // w.Stop();
172
+ // long milliseconds = w.ElapsedMilliseconds;
173
+ // Debug.Log(w.Elapsed.TotalMilliseconds + " ms");
155
174
156
175
// Another way
157
176
// property.arraySize = scriptableObjects.Length;
@@ -220,22 +239,22 @@ private static ScriptableObject[] DrawScriptableObjectSelectionControl(Rect posi
220
239
case EventType . Repaint :
221
240
if ( scriptableObjects . Length == 0 )
222
241
{
223
- _tempContent . text = "Nothing" ;
242
+ _popupContent . text = "Nothing" ;
224
243
}
225
244
else if ( scriptableObjects . Length == _scriptableObjects . Count )
226
245
{
227
- _tempContent . text = "Everything" ;
246
+ _popupContent . text = "Everything" ;
228
247
}
229
248
else if ( scriptableObjects . Length >= 2 )
230
249
{
231
- _tempContent . text = "Mixed ..." ;
250
+ _popupContent . text = "Mixed ..." ;
232
251
}
233
252
else
234
253
{
235
- _tempContent . text = scriptableObjects [ 0 ] . name ;
254
+ _popupContent . text = scriptableObjects [ 0 ] . name ;
236
255
}
237
256
238
- EditorStyles . popup . Draw ( position , _tempContent , controlID ) ;
257
+ EditorStyles . popup . Draw ( position , _popupContent , controlID ) ;
239
258
break ;
240
259
}
241
260
@@ -263,7 +282,7 @@ private static void DisplayDropDown(Rect position, ScriptableObject[] selectedSc
263
282
{
264
283
var scriptableObject = _scriptableObjects [ i ] ;
265
284
266
- string menuLabel = FormatGroupedScriptableObject ( scriptableObject , grouping ) ;
285
+ string menuLabel = MakeDropDownGroup ( scriptableObject , grouping ) ;
267
286
if ( string . IsNullOrEmpty ( menuLabel ) )
268
287
continue ;
269
288
@@ -274,7 +293,34 @@ private static void DisplayDropDown(Rect position, ScriptableObject[] selectedSc
274
293
menu . DropDown ( position ) ;
275
294
}
276
295
277
- private static string FindPath ( ScriptableObject scriptableObject )
296
+ private static void OnSelectedScriptableObject ( object userData )
297
+ {
298
+ if ( userData == null )
299
+ {
300
+ _selectedScriptableObjects . Clear ( ) ;
301
+ }
302
+ else if ( userData . GetType ( ) . IsArray )
303
+ {
304
+ _selectedScriptableObjects = ( userData as ScriptableObject [ ] ) . ToList ( ) ;
305
+ }
306
+ else
307
+ {
308
+ ScriptableObject scriptableObject = userData as ScriptableObject ;
309
+ if ( ! ResolveSelectedScriptableObject ( scriptableObject ) )
310
+ {
311
+ _selectedScriptableObjects . Add ( scriptableObject ) ;
312
+ }
313
+ else
314
+ {
315
+ _selectedScriptableObjects . Remove ( scriptableObject ) ;
316
+ }
317
+ }
318
+
319
+ var scriptableObjectReferenceUpdatedEvent = EditorGUIUtility . CommandEvent ( "ScriptableObjectReferenceUpdated" ) ;
320
+ EditorWindow . focusedWindow . SendEvent ( scriptableObjectReferenceUpdatedEvent ) ;
321
+ }
322
+
323
+ private static string FindScriptableObjectFolderPath ( ScriptableObject scriptableObject )
278
324
{
279
325
string path = AssetDatabase . GetAssetPath ( scriptableObject ) ;
280
326
path = path . Substring ( path . IndexOf ( "Resources" ) ) ;
@@ -284,9 +330,9 @@ private static string FindPath(ScriptableObject scriptableObject)
284
330
return path ;
285
331
}
286
332
287
- private static string FormatGroupedScriptableObject ( ScriptableObject scriptableObject , ScriptableObjectGrouping grouping )
333
+ private static string MakeDropDownGroup ( ScriptableObject scriptableObject , ScriptableObjectGrouping grouping )
288
334
{
289
- string path = FindPath ( scriptableObject ) ;
335
+ string path = FindScriptableObjectFolderPath ( scriptableObject ) ;
290
336
291
337
switch ( grouping )
292
338
{
@@ -306,32 +352,5 @@ private static string FormatGroupedScriptableObject(ScriptableObject scriptableO
306
352
return path ;
307
353
}
308
354
}
309
-
310
- private static void OnSelectedScriptableObject ( object userData )
311
- {
312
- if ( userData == null )
313
- {
314
- _selectedScriptableObjects . Clear ( ) ;
315
- }
316
- else if ( userData . GetType ( ) . IsArray )
317
- {
318
- _selectedScriptableObjects = ( userData as ScriptableObject [ ] ) . ToList ( ) ;
319
- }
320
- else
321
- {
322
- ScriptableObject scriptableObject = userData as ScriptableObject ;
323
- if ( ! ResolveSelectedScriptableObject ( scriptableObject ) )
324
- {
325
- _selectedScriptableObjects . Add ( scriptableObject ) ;
326
- }
327
- else
328
- {
329
- _selectedScriptableObjects . Remove ( scriptableObject ) ;
330
- }
331
- }
332
-
333
- var scriptableObjectReferenceUpdatedEvent = EditorGUIUtility . CommandEvent ( "ScriptableObjectReferenceUpdated" ) ;
334
- EditorWindow . focusedWindow . SendEvent ( scriptableObjectReferenceUpdatedEvent ) ;
335
- }
336
355
}
337
356
}
0 commit comments