@@ -30,14 +30,17 @@ protected override void OnEnable() {
30
30
}
31
31
32
32
public class CustomEditorBase : Editor {
33
- protected Dictionary < string , Action < SerializedProperty > > _specifiedDrawers ;
33
+ protected Dictionary < string , Action < SerializedProperty > > _specifiedDrawers ;
34
34
protected Dictionary < string , List < Action < SerializedProperty > > > _specifiedDecorators ;
35
35
protected Dictionary < string , List < Action < SerializedProperty > > > _specifiedPostDecorators ;
36
- protected Dictionary < string , List < Func < bool > > > _conditionalProperties ;
37
- protected List < string > _deferredProperties ;
36
+ protected Dictionary < string , List < Func < bool > > > _conditionalProperties ;
37
+ protected Dictionary < string , List < string > > _foldoutProperties ;
38
+ protected Dictionary < string , bool > _foldoutStates ;
39
+ protected List < string > _deferredProperties ;
38
40
protected bool _showScriptField = true ;
39
41
40
42
private bool _canCallSpecifyFunctions = false ;
43
+ private GUIStyle _boldFoldoutStyle ;
41
44
42
45
protected List < SerializedProperty > _modifiedProperties = new List < SerializedProperty > ( ) ;
43
46
@@ -180,6 +183,37 @@ protected void deferProperty(string propertyName) {
180
183
_deferredProperties . Insert ( 0 , propertyName ) ;
181
184
}
182
185
186
+ /// <summary>
187
+ /// Condition the drawing of a property based on the status of a foldout drop-down.
188
+ /// </summary>
189
+ protected void addPropertyToFoldout ( string propertyName , string foldoutName , bool foldoutStartOpen = false ) {
190
+ throwIfNotInOnEnable ( "addPropertyToFoldout" ) ;
191
+
192
+ if ( ! validateProperty ( propertyName ) ) { return ; }
193
+
194
+ List < string > list ;
195
+ if ( ! _foldoutProperties . TryGetValue ( foldoutName , out list ) ) {
196
+ list = new List < string > ( ) ;
197
+ _foldoutProperties [ foldoutName ] = list ;
198
+ }
199
+ _foldoutProperties [ foldoutName ] . Add ( propertyName ) ;
200
+ _foldoutStates [ foldoutName ] = foldoutStartOpen ;
201
+ }
202
+
203
+ /// <summary>
204
+ /// Check whether a property is inside of a foldout drop-down.
205
+ /// </summary>
206
+ protected bool isInFoldout ( string propertyName ) {
207
+ bool isInFoldout = false ;
208
+ foreach ( var foldout in _foldoutProperties ) {
209
+ foreach ( string property in foldout . Value ) {
210
+ if ( property . Equals ( propertyName ) ) { isInFoldout = true ; break ; }
211
+ }
212
+ if ( isInFoldout ) { break ; }
213
+ }
214
+ return isInFoldout ;
215
+ }
216
+
183
217
protected void drawScriptField ( bool disable = true ) {
184
218
var scriptProp = serializedObject . FindProperty ( "m_Script" ) ;
185
219
EditorGUI . BeginDisabledGroup ( disable ) ;
@@ -195,11 +229,13 @@ protected virtual void OnEnable() {
195
229
throw new Exception ( "Cleaning up an editor of type " + GetType ( ) + ". Make sure to always destroy your editors when you are done with them!" ) ;
196
230
}
197
231
198
- _specifiedDrawers = new Dictionary < string , Action < SerializedProperty > > ( ) ;
199
- _specifiedDecorators = new Dictionary < string , List < Action < SerializedProperty > > > ( ) ;
232
+ _specifiedDrawers = new Dictionary < string , Action < SerializedProperty > > ( ) ;
233
+ _specifiedDecorators = new Dictionary < string , List < Action < SerializedProperty > > > ( ) ;
200
234
_specifiedPostDecorators = new Dictionary < string , List < Action < SerializedProperty > > > ( ) ;
201
- _conditionalProperties = new Dictionary < string , List < Func < bool > > > ( ) ;
202
- _deferredProperties = new List < string > ( ) ;
235
+ _conditionalProperties = new Dictionary < string , List < Func < bool > > > ( ) ;
236
+ _foldoutProperties = new Dictionary < string , List < string > > ( ) ;
237
+ _foldoutStates = new Dictionary < string , bool > ( ) ;
238
+ _deferredProperties = new List < string > ( ) ;
203
239
_canCallSpecifyFunctions = true ;
204
240
}
205
241
@@ -216,6 +252,12 @@ protected bool validateProperty(string propertyName) {
216
252
* Individual properties can be specified to have custom drawers.
217
253
*/
218
254
public override void OnInspectorGUI ( ) {
255
+ // OnInspectorGUI is the first time "EditorStyles" can be accessed
256
+ if ( _boldFoldoutStyle == null ) {
257
+ _boldFoldoutStyle = new GUIStyle ( EditorStyles . foldout ) ;
258
+ _boldFoldoutStyle . fontStyle = FontStyle . Bold ;
259
+ }
260
+
219
261
_canCallSpecifyFunctions = false ;
220
262
221
263
_modifiedProperties . Clear ( ) ;
@@ -228,7 +270,8 @@ public override void OnInspectorGUI() {
228
270
continue ;
229
271
}
230
272
231
- if ( _deferredProperties . Contains ( iterator . name ) ) {
273
+ if ( _deferredProperties . Contains ( iterator . name ) ||
274
+ isInFoldout ( iterator . name ) ) {
232
275
continue ;
233
276
}
234
277
@@ -240,7 +283,28 @@ public override void OnInspectorGUI() {
240
283
}
241
284
242
285
foreach ( var deferredProperty in _deferredProperties ) {
243
- drawProperty ( serializedObject . FindProperty ( deferredProperty ) ) ;
286
+ if ( ! isInFoldout ( deferredProperty ) ) {
287
+ drawProperty ( serializedObject . FindProperty ( deferredProperty ) ) ;
288
+ }
289
+ }
290
+
291
+ foreach ( var foldout in _foldoutProperties ) {
292
+ _foldoutStates [ foldout . Key ] =
293
+ EditorGUILayout . Foldout ( _foldoutStates [ foldout . Key ] , foldout . Key , _boldFoldoutStyle ) ;
294
+ if ( _foldoutStates [ foldout . Key ] ) {
295
+ // Draw normal priority properties first
296
+ foreach ( var property in foldout . Value ) {
297
+ if ( ! _deferredProperties . Contains ( property ) ) {
298
+ drawProperty ( serializedObject . FindProperty ( property ) ) ;
299
+ }
300
+ }
301
+ // Draw deferred properties second
302
+ foreach ( var property in foldout . Value ) {
303
+ if ( _deferredProperties . Contains ( property ) ) {
304
+ drawProperty ( serializedObject . FindProperty ( property ) ) ;
305
+ }
306
+ }
307
+ }
244
308
}
245
309
246
310
serializedObject . ApplyModifiedProperties ( ) ;
0 commit comments