@@ -19,7 +19,7 @@ public class ScriptableObjectMultiSelectionDropdownDrawer : PropertyDrawer
1919 private static List < ScriptableObject > _scriptableObjects = new List < ScriptableObject > ( ) ;
2020 private static List < ScriptableObject > _selectedScriptableObjects = new List < ScriptableObject > ( ) ;
2121 private static readonly int _controlHint = typeof ( ScriptableObjectMultiSelectDropdownAttribute ) . GetHashCode ( ) ;
22- private static GUIContent _tempContent = new GUIContent ( ) ;
22+ private static GUIContent _popupContent = new GUIContent ( ) ;
2323 private static int _selectionControlID ;
2424 private static readonly GenericMenu . MenuFunction2 _onSelectedScriptableObject = OnSelectedScriptableObject ;
2525 private static bool isChanged ;
@@ -46,6 +46,9 @@ public override float GetPropertyHeight(SerializedProperty property, GUIContent
4646 return EditorStyles . popup . CalcHeight ( GUIContent . none , 0 ) ;
4747 }
4848
49+ /// <summary>
50+ /// How you can get type of field which it uses PropertyAttribute
51+ /// </summary>
4952 private static Type GetPropertyType ( SerializedProperty property )
5053 {
5154 Type parentType = property . serializedObject . targetObject . GetType ( ) ;
@@ -71,11 +74,17 @@ private static bool ValidateProperty(SerializedProperty property)
7174 return true ;
7275 }
7376
77+ /// <summary>
78+ /// When new ScriptableObject added to the project
79+ /// </summary>
7480 private static void ClearCache ( )
7581 {
7682 _scriptableObjects . Clear ( ) ;
7783 }
7884
85+ /// <summary>
86+ /// Gets ScriptableObjects just when it is a first time or new ScriptableObject added to the project
87+ /// </summary>
7988 private static void GetScriptableObjects ( ScriptableObjectMultiSelectDropdownAttribute attribute )
8089 {
8190 UnityEngine . Object [ ] loadedObject = Resources . LoadAll ( "" , attribute . BaseType ) ;
@@ -85,6 +94,9 @@ private static void GetScriptableObjects(ScriptableObjectMultiSelectDropdownAttr
8594 }
8695 }
8796
97+ /// <summary>
98+ /// Checks if the ScriptableObject is selected or not by checking if the list contains it.
99+ /// </summary>
88100 private static bool ResolveSelectedScriptableObject ( ScriptableObject scriptableObject )
89101 {
90102 if ( _selectedScriptableObjects == null )
@@ -117,6 +129,9 @@ private static void Draw(Rect position, GUIContent label,
117129 }
118130 }
119131
132+ /// <summary>
133+ /// Iterats through the property for finding selected ScriptableObjects
134+ /// </summary>
120135 private static ScriptableObject [ ] Read ( SerializedProperty property )
121136 {
122137 List < ScriptableObject > selectedScriptableObjects = new List < ScriptableObject > ( ) ;
@@ -133,10 +148,14 @@ private static ScriptableObject[] Read(SerializedProperty property)
133148 return selectedScriptableObjects . ToArray ( ) ;
134149 }
135150
151+ /// <summary>
152+ /// Iterats through the property for storing selected ScriptableObjects
153+ /// </summary>
136154 private static void Write ( SerializedProperty property , ScriptableObject [ ] scriptableObjects )
137155 {
138- //var w = new System.Diagnostics.Stopwatch();
139- //w.Start();
156+ // Faster way
157+ // var w = new System.Diagnostics.Stopwatch();
158+ // w.Start();
140159 int i = 0 ;
141160 SerializedProperty iterator = property . Copy ( ) ;
142161 iterator . arraySize = scriptableObjects . Length ;
@@ -149,9 +168,9 @@ private static void Write(SerializedProperty property, ScriptableObject[] script
149168 i ++ ;
150169 }
151170 }
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");
155174
156175 // Another way
157176 // property.arraySize = scriptableObjects.Length;
@@ -220,22 +239,22 @@ private static ScriptableObject[] DrawScriptableObjectSelectionControl(Rect posi
220239 case EventType . Repaint :
221240 if ( scriptableObjects . Length == 0 )
222241 {
223- _tempContent . text = "Nothing" ;
242+ _popupContent . text = "Nothing" ;
224243 }
225244 else if ( scriptableObjects . Length == _scriptableObjects . Count )
226245 {
227- _tempContent . text = "Everything" ;
246+ _popupContent . text = "Everything" ;
228247 }
229248 else if ( scriptableObjects . Length >= 2 )
230249 {
231- _tempContent . text = "Mixed ..." ;
250+ _popupContent . text = "Mixed ..." ;
232251 }
233252 else
234253 {
235- _tempContent . text = scriptableObjects [ 0 ] . name ;
254+ _popupContent . text = scriptableObjects [ 0 ] . name ;
236255 }
237256
238- EditorStyles . popup . Draw ( position , _tempContent , controlID ) ;
257+ EditorStyles . popup . Draw ( position , _popupContent , controlID ) ;
239258 break ;
240259 }
241260
@@ -263,7 +282,7 @@ private static void DisplayDropDown(Rect position, ScriptableObject[] selectedSc
263282 {
264283 var scriptableObject = _scriptableObjects [ i ] ;
265284
266- string menuLabel = FormatGroupedScriptableObject ( scriptableObject , grouping ) ;
285+ string menuLabel = MakeDropDownGroup ( scriptableObject , grouping ) ;
267286 if ( string . IsNullOrEmpty ( menuLabel ) )
268287 continue ;
269288
@@ -274,7 +293,34 @@ private static void DisplayDropDown(Rect position, ScriptableObject[] selectedSc
274293 menu . DropDown ( position ) ;
275294 }
276295
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 )
278324 {
279325 string path = AssetDatabase . GetAssetPath ( scriptableObject ) ;
280326 path = path . Substring ( path . IndexOf ( "Resources" ) ) ;
@@ -284,9 +330,9 @@ private static string FindPath(ScriptableObject scriptableObject)
284330 return path ;
285331 }
286332
287- private static string FormatGroupedScriptableObject ( ScriptableObject scriptableObject , ScriptableObjectGrouping grouping )
333+ private static string MakeDropDownGroup ( ScriptableObject scriptableObject , ScriptableObjectGrouping grouping )
288334 {
289- string path = FindPath ( scriptableObject ) ;
335+ string path = FindScriptableObjectFolderPath ( scriptableObject ) ;
290336
291337 switch ( grouping )
292338 {
@@ -306,32 +352,5 @@ private static string FormatGroupedScriptableObject(ScriptableObject scriptableO
306352 return path ;
307353 }
308354 }
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- }
336355 }
337356}
0 commit comments