- Home /
What is the efficient way of showing variables depend on enum in custom inspector?
I want the inspector to show different variable depends on the enum you choose. I know i need to use custom inspector but there are some general variables i want to show that don't depend on the enum and always showing. How can i achieve it and what the general and efficient way of showing custom variable?
Answer by Kishotta · Feb 28, 2020 at 08:43 PM
You can do this with some conditional control flow in a custom inspector:
public enum Group { One, Two }
public class Client : MonoBehaviour {
public Group Group;
public bool GroupOneData;
public float GroupTwoData;
public string IndependentData;
}
[CustomEditor (typeof (Client))]
public class ClientEditor : Editor {
void OnInspectorGUI () {
Client client = (Client)target;
// Display dropdown
client.Group = (Group)EditorGUILayout.EnumPopup ("Group", client.Group);
// Display conditional for one
if (client.Group == Group.One) {
client.GroupOneData = EditorGUILayout.Toggle ("Bool", client.GroupOneData);
}
// Display conditional for two
if (client.Group == Group.Two) {
client.GroupTwoData = EditorGUILayout.FloatField("Float", client.GroupTwoData);
}
// Display always
client.IndependentData = EditorGUILayout.TextField ("String", client.IndependentData);
}
}
When One
is selected for Group, the "Bool" toggle will appear. When Two
is selected for Group, the "Float" field will appear. The "String" text field will appear regardless of which option is selected. If, in the future, you add an additional Three
option to the group enum, neight the "Bool" nor "Float" fields will appear if it is selected. "String" will appear no matter what.
what is the difference between editorguilayout and guilayout? Also what are the method for other data types like gameobject, int, transform, my custom script?
Hi, there's something not working in this script. I've created a Script with all the code shown above, but it seems not considering the Client CustomEditor Class: is this code working for anyone else?
Answer by Zentiu · Feb 28, 2020 at 05:21 PM
When you have an enum with some variables in it, for your class use :
public enum bla {blie, yay, boo};
public class blu : MonoBehaviour
{
}
Something fucked up...
again..
public enum bla {blie, yay, boo};
public class blu : $$anonymous$$onoBehaviour
{
public bla enumValue;
}
This should be able to show the enum in the inspector basicly.
No, i want to show different variables depend on the enum you choose. Also can u answer my question from the answer above.