- Home /
Inspector - Show/hide property within custom class using enum
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?
I'm not sure if that's the actual problem,
but i guess the problem is this line:
Generator.SegmentSeparator segmentSeparator = (Generator.SegmentSeparator)subObject.FindPropertyRelative("useSeparator").enumValueIndex;
I'm pretty sure that "enumValueIndex" does not equal the enum value but only the index of the available enum values which you could get from Enum.GetValues. The index does not necessarily represent the actual value. Can you include your enum declaration?
Try "intValue" ins$$anonymous$$d. I'm not sure if it will return the actual value of an enum, but under the hood an enum is just an integer.
edit
Ohh, i just saw the "seperator" at the end of your third screenshot. ^^ Ok so the enum at least works as expected. I'll post an answer since i found your problem ^^.
Answer by Bunny83 · Mar 07, 2017 at 12:32 AM
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.
Your answer
Follow this Question
Related Questions
What is the efficient way of showing variables depend on enum in custom inspector? 2 Answers
Creating enum using a string array 3 Answers
Expand Component on Inspector 0 Answers
How do I change inspector values of other game objects using a custom Editor Window 0 Answers
Getting the selected enum value set in the Inspector 2 Answers