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
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 ();
}
}
For that answer’s code, is it possible to have an intfield rather than an intslider?