Hi,
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 () {
serializedObject.Update();
EditorGUILayout.PropertyField(serializedObject.FindProperty("optionSource"));
Generator.OptionSource optionSource = (Generator.OptionSource)serializedObject.FindProperty("optionSource").enumValueIndex;
switch (optionSource) {
case Generator.OptionSource.External:
EditorGUILayout.PropertyField(serializedObject.FindProperty("optionList") );
break;
case Generator.OptionSource.Mixed:
EditorGUILayout.PropertyField(serializedObject.FindProperty("optionList") );
break;
}
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") );
break;
}
}
serializedObject.ApplyModifiedProperties();
}
}
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?