Inspector - Show/hide property within custom class using enum


I’ve been digging around for quite awhile now trying to figure out a solution to this problem.

In the inspector, I have a custom class, Segments. Segments has a number of variables including an enum meant to select between a number of options. Based on what the enum is set to in the inspector, I want it to show or hide different options the user can access.

I’ve attached a screenshot to show what I’d like to see vs. what’s actually happening.

Here’s the code I’m using for the editor script:

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;

[CustomEditor (typeof(NameGenerator))]
public class NameGeneratorEditor : Editor {

	public override void OnInspectorGUI () {


		Generator.OptionSource optionSource = (Generator.OptionSource)serializedObject.FindProperty("optionSource").enumValueIndex;

		switch (optionSource) {
		case Generator.OptionSource.External:
			EditorGUILayout.PropertyField(serializedObject.FindProperty("optionList") );

		case Generator.OptionSource.Mixed:
			EditorGUILayout.PropertyField(serializedObject.FindProperty("optionList") );

		EditorGUILayout.PropertyField(serializedObject.FindProperty("randomizeOnStart") );
		EditorGUILayout.PropertyField(serializedObject.FindProperty("segments"), true);

		SerializedProperty sp = serializedObject.FindProperty("segments");
		for (int x = 0; x < sp.arraySize; x++) {
			SerializedProperty subObject = sp.GetArrayElementAtIndex(x);

			Generator.SegmentSeparator segmentSeparator = (Generator.SegmentSeparator)subObject.FindPropertyRelative("useSeparator").enumValueIndex;

			switch (segmentSeparator) {

			case Generator.SegmentSeparator.Custom:

				EditorGUILayout.PropertyField(subObject.FindPropertyRelative("separator") );


Note: I’ve got a working version of this towards the top of the script with the OptionSource enum (only this one is not contained within a custom class".

The values for “Use Separator” are ‘Custom’, ‘Default’ and ‘None’. When the enum is set to ‘Custom’ I want it to show the ‘Separator’ field for that item only. It’s currently showing the proper field, only it’s not showing it in the right spot.

I think the solution is somewhere within the “EditorGUILayout.PropertyField(subObject.FindPropertyRelative(“separator”) );” line, but I just don’t know how to get there.

Any suggestions?

Ok the problem is that you iterate through the “segments” after all the segments have already been drawn. That’s because you draw the segments in one line:

EditorGUILayout.PropertyField(serializedObject.FindProperty("segments"), true);

This single PropertyField will process the complete array since you pass “true” as parameter. If you want to “insert” or “change” how the sub elements are drawn you have to iterate the properties “manually”. A SerializedProperty acts like an iterator. You can use the Next or NextVisible method to get the next SerializedProperty. Note the parameter “enterChildren”.

However, is your “separator” field actually serialized? If it is it should actually show up by default. The best approach to hide certain propertes conditionally is to simply skip the property if the condition is not right when you actually stumble on that property.

Note that when using Next / NextVisible you will basically iterate “line by line” of the original inspector. So the first “child” of “segments” would be the “Size” field of the array. The next one would be the “Element0” header and so on.