11using System ;
2- using System . Collections . Generic ;
3- using System . Linq ;
42using UnityEditor ;
53using UnityEditor . IMGUI . Controls ;
64using UnityEngine ;
@@ -27,30 +25,12 @@ private CollectionItemEditorOptionsAttribute OptionsAttribute
2725 }
2826
2927 private bool initialized ;
30- private ScriptableObjectCollection collection ;
3128
32- private ScriptableObjectCollectionItem [ ] options ;
33- private GUIContent [ ] guiContents ;
34-
35- private ScriptableObjectCollectionItem item ;
3629 private Object currentObject ;
3730
3831 private CollectionItemDropdown collectionItemDropdown ;
32+ private ScriptableObjectCollectionItem item ;
3933
40- private ScriptableObjectCollection [ ] availableCollections ;
41- private GUIContent [ ] availableCollectionsGUIContents ;
42-
43- private bool singleCollection ;
44- private CollectionsDropDown collectionsDropdown ;
45-
46- ~ CollectionItemItemObjectPropertyDrawer ( )
47- {
48- if ( item . IsNull ( ) )
49- return ;
50-
51- ObjectUtility . SetDirty ( item ) ;
52- }
53-
5434 private CollectionItemEditorOptionsAttribute GetOptionsAttribute ( )
5535 {
5636 if ( fieldInfo == null )
@@ -62,14 +42,6 @@ object[] attributes
6242 return DefaultAttribute ;
6343 }
6444
65- public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
66- {
67- if ( singleCollection || OptionsAttribute . DrawType == DrawType . AsReference )
68- return base . GetPropertyHeight ( property , label ) ;
69-
70- return ( EditorGUIUtility . singleLineHeight + EditorGUIUtility . standardVerticalSpacing ) * 2 ;
71- }
72-
7345 public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
7446 {
7547 Initialize ( property ) ;
@@ -93,13 +65,6 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
9365 int indent = EditorGUI . indentLevel ;
9466 EditorGUI . indentLevel = 0 ;
9567
96-
97- if ( ! singleCollection )
98- {
99- DrawCollectionDropDown ( ref popupRect , property ) ;
100- popupRect . y += EditorGUIUtility . singleLineHeight + EditorGUIUtility . standardVerticalSpacing ;
101- }
102-
10368 if ( item != null )
10469 {
10570 DrawEditFoldoutButton ( ref popupRect ) ;
@@ -137,97 +102,28 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
137102 }
138103 }
139104
140- private void DrawCollectionDropDown ( ref Rect popupRect , SerializedProperty property )
141- {
142- int index = 0 ;
143- if ( collection != null )
144- index = Array . IndexOf ( availableCollections , collection ) + 1 ;
145-
146- if ( GUI . Button ( popupRect , availableCollectionsGUIContents [ index ] , EditorStyles . popup ) )
147- {
148- collectionsDropdown . Show ( popupRect , objectCollection =>
149- {
150- OnCollectionChanged ( objectCollection , property ) ;
151- }
152- ) ;
153- }
154- }
155-
156- private void OnCollectionChanged ( ScriptableObjectCollection scriptableObjectCollection , SerializedProperty property )
157- {
158- if ( scriptableObjectCollection == null )
159- {
160- collection = null ;
161- property . objectReferenceValue = null ;
162- property . serializedObject . ApplyModifiedProperties ( ) ;
163- return ;
164- }
165-
166- collection = scriptableObjectCollection ;
167- options = collection . Items . ToArray ( ) ;
168- List < string > displayOptions = collection . Items . Select ( o => o . name ) . ToList ( ) ;
169- displayOptions . Insert ( 0 , CollectionEditorGUI . DEFAULT_NONE_ITEM_TEXT ) ;
170-
171- string [ ] optionsNames = displayOptions . ToArray ( ) ;
172- guiContents = optionsNames . Select ( s => new GUIContent ( s ) ) . ToArray ( ) ;
173- collectionItemDropdown = new CollectionItemDropdown ( new AdvancedDropdownState ( ) , collection ) ;
174- }
175-
176105 private void Initialize ( SerializedProperty property )
177106 {
178107 if ( initialized )
179108 return ;
180109 Type arrayOrListType = fieldInfo . FieldType . GetArrayOrListType ( ) ;
181110 Type itemType = arrayOrListType != null ? arrayOrListType : fieldInfo . FieldType ;
182-
183- availableCollections = CollectionsRegistry . Instance . GetCollectionsByItemType ( itemType ) . ToArray ( ) ;
184- if ( availableCollections . Length == 0 )
185- {
186- OptionsAttribute . DrawType = DrawType . AsReference ;
187- return ;
188- }
189-
190- if ( availableCollections . Length == 1 )
191- {
192- singleCollection = true ;
193- OnCollectionChanged ( availableCollections . First ( ) , property ) ;
194- }
195- else
196- {
197- List < GUIContent > collectionsNames = availableCollections
198- . Select ( objectCollection => new GUIContent ( objectCollection . name ) )
199- . ToList ( ) ;
200-
201- collectionsNames . Insert ( 0 , new GUIContent ( "None" ) ) ;
202- availableCollectionsGUIContents = collectionsNames . ToArray ( ) ;
203-
204- collectionsDropdown = new CollectionsDropDown ( new AdvancedDropdownState ( ) , availableCollections ) ;
205-
206- if ( property . objectReferenceValue != null )
207- {
208- if ( property . objectReferenceValue is ScriptableObjectCollectionItem collectionItem )
209- {
210- OnCollectionChanged ( collectionItem . Collection , property ) ;
211- }
212- }
213- }
214111
112+ collectionItemDropdown = new CollectionItemDropdown (
113+ new AdvancedDropdownState ( ) ,
114+ itemType
115+ ) ;
116+
215117 currentObject = property . serializedObject . targetObject ;
216118 initialized = true ;
217119 }
218120
219121 private void DrawCollectionItemDropDown ( ref Rect position , SerializedProperty property )
220122 {
221- bool guiEnabled = GUI . enabled ;
222- if ( collection == null )
223- GUI . enabled = false ;
224- GUIContent displayValue = new GUIContent ( "Select Collection First" ) ;
123+ GUIContent displayValue = new GUIContent ( "None" ) ;
225124
226- if ( collection != null )
227- {
228- int selectedIndex = Array . IndexOf ( options , item ) + 1 ;
229- displayValue = guiContents [ selectedIndex ] ;
230- }
125+ if ( item != null )
126+ displayValue = new GUIContent ( item . name ) ;
231127
232128 if ( GUI . Button ( position , displayValue , EditorStyles . popup ) )
233129 {
@@ -237,8 +133,6 @@ private void DrawCollectionItemDropDown(ref Rect position, SerializedProperty pr
237133 property . serializedObject . ApplyModifiedProperties ( ) ;
238134 } ) ;
239135 }
240-
241- GUI . enabled = guiEnabled ;
242136 }
243137
244138 private void DrawGotoButton ( ref Rect popupRect )
@@ -250,8 +144,8 @@ private void DrawGotoButton(ref Rect popupRect)
250144 buttonRect . x += popupRect . width ;
251145 if ( GUI . Button ( buttonRect , CollectionEditorGUI . ARROW_RIGHT_CHAR ) )
252146 {
253- Selection . activeObject = collection ;
254- CollectionUtility . SetFoldoutOpen ( true , item , collection ) ;
147+ Selection . activeObject = item . Collection ;
148+ CollectionUtility . SetFoldoutOpen ( true , item , item . Collection ) ;
255149 }
256150 }
257151
0 commit comments