- Home /
Displaying public variables in the inspector
What I want to do: I have public variables that are showing in the inspector, but some of them aren't necessary to edit unless another variable (bool) is set to true.
Simple algorithm for what I want to do:
public bool isActive;
public int var1;
public int var2;
if(isActive)
{
//show var1 and var2 in the inspector
}
else
{
//do not show var1 and var2 in the inspector
}
I know this isn't really a necessary feature, but it would make it look so much better.
Edit: if it's still unclear what I want to do, what I want is for variables to show up in the inspector if a bool is set to true in the inspector.
Answer by Graham-Dunnett · Jul 08, 2014 at 12:56 PM
Write your own inspector, see:
http://docs.unity3d.com/ScriptReference/Editor.OnInspectorGUI.html
Answer by DESTRUKTORR · Jul 08, 2014 at 01:03 PM
What you probably want is either a PropertyDrawer or a custom Editor.
In either OnGUI (for a PropertyDrawer) or OnInspectorGUI (for a custom inspector Editor), you will want to use a conditional through (most likely) a toggle. There are several methods that make GUI toggles, found in GUI, GUILayout, EditorGUI, and EditorGUILayout.
Basically, contain the parts you want to have visible depending on the state of these booleans within an if statement, using said booleans as the conditions for said if statement, like so:
if(myTarget.isActive)
{
// Some GUI method
}
Within the editor or property drawer, respectively.