- Home /
Editor: How to display properties in inspector based on enum in same script ?
I have a Enum say playerType{X,Y,Z}. i want the properties of the script to be displayed based on the type of the player. Similar to "Image Type" of Image component. how to write editor script for this ?
For ex: if type= X; string Xname; Gameobject Xaddon;
if type=y; string Yname; Gameobject Yaddon;
if type=z; string Zname; Zbehaviour Ztype; (Say Zbehaviour is some enum)
Answer by Bunny83 · Dec 03, 2016 at 01:37 PM
First of all you should understand that a custom inspector can only change how the properties are displayed. They all still need to exist in the actual class. In your case you can reuse certain variables since only one "type" is valid at a time.
For example:
public class MyTest : MonoBehavioud
{
public PlayerType type;
public string objName; // shared between type X, Y and Z
public GameObject addon; // shared between type X and Y
public ZBehaviour zType;
}
In the custom inspector you then can show different GUI depending on the set type:
[CustomEditor(typeof(MyTest))]
public class MyTestEditor : Editor
{
MyTest mTest;
void OnEnable()
{
mTest = (MyTest)target;
}
public override void OnInspectorGUI()
{
mTest.type = (PlayerType)EditorGUILayout.EnumPopup("PlayerType", mTest.type);
switch(mTest.type)
{
case PlayerType.X:
{
mTest.objName = EditorGUILayout.TextField("Name", mTest.objName);
mTest.addon = (GameObject)EditorGUILayout.ObjectField("Addon", mTest.addon, typeof(GameObject), true);
break;
}
case PlayerType.Y:
{
mTest.objName = EditorGUILayout.TextField("Name", mTest.objName);
mTest.addon = (GameObject)EditorGUILayout.ObjectField("Addon", mTest.addon, typeof(GameObject), true);
break;
}
case PlayerType.Z:
{
mTest.objName = EditorGUILayout.TextField("Name", mTest.objName);
mTest.zType = (ZBehaviour)EditorGUILayout.EnumPopup("ZType", mTest.zType);
break;
}
}
}
}
Note that you could use seperate variables for your name and addon variables for each type but it wouldn't make much sense if you want to switch between the different types and only one type is in use at a time.
ps: Keep in mind that a MonoBehaviour already has a "name" property so i renamed the variable to "objName".
Is it possible to do this with a Scriptable Object? I've had no success trying to replicate with a Scriptable.