77namespace BrunoMikoski . ScriptableObjectCollections
88{
99 [ CustomPropertyDrawer ( typeof ( ScriptableObjectCollectionItem ) , true ) ]
10- public class CollectionItemItemObjectPropertyDrawer : PropertyDrawer
10+ public class CollectionItemItemPropertyDrawer : PropertyDrawer
1111 {
1212 private static readonly CollectionItemEditorOptionsAttribute DefaultAttribute
1313 = new CollectionItemEditorOptionsAttribute ( DrawType . Dropdown ) ;
@@ -41,12 +41,11 @@ object[] attributes
4141 return attributes [ 0 ] as CollectionItemEditorOptionsAttribute ;
4242 return DefaultAttribute ;
4343 }
44-
44+
4545 public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
4646 {
4747 Initialize ( property ) ;
4848
49-
5049 if ( OptionsAttribute . DrawType == DrawType . AsReference )
5150 {
5251 EditorGUI . PropertyField ( position , property , label , true ) ;
@@ -55,57 +54,68 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
5554
5655 item = property . objectReferenceValue as ScriptableObjectCollectionItem ;
5756
58- Rect popupRect = position ;
59- popupRect . height = 15 ;
57+ DrawCollectionItemDrawer ( position , item , label ,
58+ newItem =>
59+ {
60+ property . objectReferenceValue = newItem ;
61+ property . serializedObject . ApplyModifiedProperties ( ) ;
62+ } ) ;
63+ }
6064
61- using ( new EditorGUI . PropertyScope ( position , label , property ) )
65+ internal void DrawCollectionItemDrawer ( Rect position , ScriptableObjectCollectionItem collectionItem , GUIContent label ,
66+ Action < ScriptableObjectCollectionItem > callback )
67+ {
68+ position . height = 15 ;
69+ position = EditorGUI . PrefixLabel ( position , label ) ;
70+ int indent = EditorGUI . indentLevel ;
71+ EditorGUI . indentLevel = 0 ;
72+ if ( collectionItem != null )
6273 {
63- popupRect = EditorGUI . PrefixLabel ( popupRect , label ) ;
74+ DrawEditFoldoutButton ( ref position ) ;
75+ DrawGotoButton ( ref position ) ;
76+ }
6477
65- int indent = EditorGUI . indentLevel ;
66- EditorGUI . indentLevel = 0 ;
78+ DrawCollectionItemDropDown ( ref position , collectionItem , callback ) ;
79+ DrawEditorPreview ( collectionItem ) ;
80+ EditorGUI . indentLevel = indent ;
81+ }
6782
68- if ( item != null )
69- {
70- DrawEditFoldoutButton ( ref popupRect ) ;
71- DrawGotoButton ( ref popupRect ) ;
72- }
73-
74- DrawCollectionItemDropDown ( ref popupRect , property ) ;
83+ private void DrawEditorPreview ( ScriptableObjectCollectionItem scriptableObjectCollectionItem )
84+ {
85+ if ( scriptableObjectCollectionItem == null )
86+ return ;
87+
88+ if ( ! CollectionUtility . IsFoldoutOpen ( scriptableObjectCollectionItem , currentObject ) )
89+ return ;
7590
76- if ( item != null )
91+ EditorGUI . indentLevel ++ ;
92+ using ( new EditorGUILayout . VerticalScope ( "Box" ) )
93+ {
94+ Editor editor = EditorsCache . GetOrCreateEditorForItem ( scriptableObjectCollectionItem ) ;
95+ using ( EditorGUI . ChangeCheckScope changedCheck = new EditorGUI . ChangeCheckScope ( ) )
7796 {
78- if ( CollectionUtility . IsFoldoutOpen ( item , currentObject ) )
97+ GUILayout . Space ( 10 ) ;
98+ using ( new EditorGUILayout . VerticalScope ( ) )
7999 {
80- EditorGUI . indentLevel ++ ;
81- using ( new EditorGUILayout . VerticalScope ( "Box" ) )
82- {
83- Editor editor = EditorsCache . GetOrCreateEditorForItem ( item ) ;
84- using ( EditorGUI . ChangeCheckScope changedCheck = new EditorGUI . ChangeCheckScope ( ) )
85- {
86- GUILayout . Space ( 10 ) ;
87- using ( new EditorGUILayout . VerticalScope ( ) )
88- {
89- editor . OnInspectorGUI ( ) ;
90- }
91-
92- EditorGUILayout . Space ( ) ;
93-
94- if ( changedCheck . changed )
95- property . serializedObject . ApplyModifiedProperties ( ) ;
96- }
97- }
98- EditorGUI . indentLevel -- ;
100+ editor . OnInspectorGUI ( ) ;
101+ }
102+
103+ EditorGUILayout . Space ( ) ;
104+ if ( changedCheck . changed )
105+ {
106+ EditorUtility . SetDirty ( scriptableObjectCollectionItem ) ;
99107 }
100108 }
101- EditorGUI . indentLevel = indent ;
102109 }
110+
111+ EditorGUI . indentLevel -- ;
103112 }
104113
105114 private void Initialize ( SerializedProperty property )
106115 {
107116 if ( initialized )
108117 return ;
118+
109119 Type arrayOrListType = fieldInfo . FieldType . GetArrayOrListType ( ) ;
110120 Type itemType = arrayOrListType != null ? arrayOrListType : fieldInfo . FieldType ;
111121
@@ -118,20 +128,33 @@ private void Initialize(SerializedProperty property)
118128 initialized = true ;
119129 }
120130
121- private void DrawCollectionItemDropDown ( ref Rect position , SerializedProperty property )
131+ internal void Initialize ( Type collectionItemType , Object obj )
132+ {
133+ if ( initialized )
134+ return ;
135+
136+ collectionItemDropdown = new CollectionItemDropdown (
137+ new AdvancedDropdownState ( ) ,
138+ collectionItemType
139+ ) ;
140+
141+ currentObject = obj ;
142+ initialized = true ;
143+ }
144+
145+ private void DrawCollectionItemDropDown (
146+ ref Rect position ,
147+ ScriptableObjectCollectionItem collectionItem ,
148+ Action < ScriptableObjectCollectionItem > callback )
122149 {
123150 GUIContent displayValue = new GUIContent ( "None" ) ;
124151
125- if ( item != null )
126- displayValue = new GUIContent ( item . name ) ;
152+ if ( collectionItem != null )
153+ displayValue = new GUIContent ( collectionItem . name ) ;
127154
128155 if ( GUI . Button ( position , displayValue , EditorStyles . popup ) )
129156 {
130- collectionItemDropdown . Show ( position , o =>
131- {
132- property . objectReferenceValue = o ;
133- property . serializedObject . ApplyModifiedProperties ( ) ;
134- } ) ;
157+ collectionItemDropdown . Show ( position , callback . Invoke ) ;
135158 }
136159 }
137160
@@ -167,5 +190,6 @@ private void DrawEditFoldoutButton(ref Rect popupRect)
167190 ObjectUtility . SetDirty ( item ) ;
168191 }
169192 }
193+
170194 }
171195}
0 commit comments