- Home /
Change Inspector Variables Depending On Enum
Hello UnityAnswers,
How would on go through in editing/changing the inspector variables depending on one variable that's an enum.
EXAMPLE: There's an enum variable called "Task" which has the options "Guard", "Patrol", or "Off-Duty".
If I select "Guard" different variables will show in the inspector specific to the guard-task. And then same for "Patrol" and "Off-Duty"
How would I go about in doing this?
Thanks in advance
Answer by Chronos-L · Mar 16, 2013 at 04:16 AM
You will have to write your own editor. Unity Custom Editor Reference
In the OnInspectorGUI()
, you can use a switch()
or if-else
to determine what inspector variable you want to show.
Example
using UnityEngine;
using System.Collections;
public class PropertyHolder : MonoBehaviour {
public enum Status { A, B, C };
public Status state;
public int valForAB;
public int valForA;
public int valForC;
public bool controllable;
void Start () {
}
void Update () {
}
}
Put this in Assets/Editor
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(PropertyHolder)), CanEditMultipleObjects]
public class PropertyHolderEditor : Editor {
public SerializedProperty
state_Prop,
valForAB_Prop,
valForA_Prop,
valForC_Prop,
controllable_Prop;
void OnEnable () {
// Setup the SerializedProperties
state_Prop = serializedObject.FindProperty ("state");
valForAB_Prop = serializedObject.FindProperty("valForAB");
valForA_Prop = serializedObject.FindProperty ("valForA");
valForC_Prop = serializedObject.FindProperty ("valForC");
controllable_Prop = serializedObject.FindProperty ("controllable");
}
public override void OnInspectorGUI() {
serializedObject.Update ();
EditorGUILayout.PropertyField( state_Prop );
PropertyHolder.Status st = (PropertyHolder.Status)state_Prop.enumValueIndex;
switch( st ) {
case PropertyHolder.Status.A:
EditorGUILayout.PropertyField( controllable_Prop, new GUIContent("controllable") );
EditorGUILayout.IntSlider ( valForA_Prop, 0, 10, new GUIContent("valForA") );
EditorGUILayout.IntSlider ( valForAB_Prop, 0, 100, new GUIContent("valForAB") );
break;
case PropertyHolder.Status.B:
EditorGUILayout.PropertyField( controllable_Prop, new GUIContent("controllable") );
EditorGUILayout.IntSlider ( valForAB_Prop, 0, 100, new GUIContent("valForAB") );
break;
case PropertyHolder.Status.C:
EditorGUILayout.PropertyField( controllable_Prop, new GUIContent("controllable") );
EditorGUILayout.IntSlider ( valForC_Prop, 0, 100, new GUIContent("valForC") );
break;
}
serializedObject.ApplyModifiedProperties ();
}
}
Some edit: It's work fine for default enumerations like:
enum DefaultEnumSample{ kWood, kSteel, kStone }
what is equal to:
enum DefaultEnumSample{ kWood = 0, kSteel = 1, kStone = 2 }
But if you use enumerator like:
enum WeaponSampleEnum{ kNone = -1, kHand = 0, kSword = 10, kBigSword = 11, k$$anonymous$$ace = 20 }
then this method doesn't work.
Solution:
just change
PropertyHolder.Status st = (PropertyHolder.Status)state_Prop.enumValueIndex;
to:
PropertyHolder.Status st = (PropertyHolder.Status)state_Prop.intValue;
Works great, just one question, how would you go about adding color fields and object fields to this? Say I want valForAB to be an object field, how would I do this? same thing for color. I seem to be getting errors because the serializedproperties are not actually object/color types. Should I just not use serializedproperty for those?
Answer by awesomeface · May 12, 2015 at 02:26 AM
For that answer's code, is it possible to have an intfield rather than an intslider?
Your answer
![](https://koobas.hobune.stream/wayback/20220613094507im_/https://answers.unity.com/themes/thub/images/avi.jpg)