- Home /
Custom Editor - Show Variable if Boolean is True.
A simple question to Cutom Editor users.
I'm trying to organize my workflow. I've got three categories: A, B and C in one script (Overall settings, Primary Fire, Alternative Fire). Each category has about ~20 different variables in the inspector and most of the time I don't need to show all of them in the inspector.
What ritual should I perform to make a Custom Inspector, which shows different variables only when another variable is set True and hides them if False? For example:
(if A == true)
{
//Show variables 1-20 in the Inspector
}
(if B == true)
{
//Show variables 21-30 in the Inspector
}
(if C == true)
{
//Show variables 31-35 in the Inspector
}
Image Example:
Answer by DoTA_KAMIKADzE · Apr 05, 2015 at 01:36 PM
Use Popup with a string array consisting of "A", "B", "C" and then check the returned value from it if "A" else if "B" else if "C"
Or if you can have all types at the same time then just utilize 3 Toggle
P.S. Here you have an updated answer for your updated question, something like this should work:
[CustomEditor(typeof(ItsDaScriptDude))]
public class ItsDaScriptDudeEditor : Editor
{
private bool AToggle = false;
private ItsDaScriptDude _evCtrl = null;
void OnEnable()
{
_evCtrl = (ItsDaScriptDude)target;
}
public override void OnInspectorGUI()
{
//blablabla
GUILayout.BeginHorizontal();
GUILayout.Label("A Features", GUILayout.Width(70));
AToggle = EditorGUILayout.Toggle(AToggle);
GUILayout.EndHorizontal();
if (AToggle)
{
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("This Var", GUILayout.Width(70));
_evCtrl.ThisVar = EditorGUILayout.TextField(_evCtrl.ThisVar);
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("And This One", GUILayout.Width(70));
_evCtrl.AndThisOne = EditorGUILayout.TextField(_evCtrl.AndThisOne);
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("And This Can Be Slider", GUILayout.Width(70));
_evCtrl.AndThisCanBeSlider = EditorGUILayout.Slider(_evCtrl.AndThisCanBeSlider, 0f, 100f);
GUILayout.EndHorizontal();
}
//blablabla
}
}
Ha, great find! The Toggle option almost answers my question! It gives the answer to "How to make "(if A == true)" part, so 50% done. But what exact command can show or hide the variables in each category? :)
There is no command to show/hide, Inspector "refreshes" every time something changes, so think about it like Update() function works for game, it just iterates trough that OnInspectorGUI() over and over again so if code does not reach some controls because your "if" was not true then that part of code will just not run - thus everything under that if statement will run only if that if is true.
P.S. Just in case I got you wrong and if you meant which one to use to show variables, then just FIND HERE anything that suits you more, for example:
EditorGUILayout.ObjectField
EditorGUILayout.TextField
Just to be exact - updated the First Post with an image of what I'm trying to do in the Inspector.
Your answer
Follow this Question
Related Questions
How to handle Serializable classes in a CustomEditor? 0 Answers
Initializing 2D array via inspector 3 Answers
Scrollbar in Inspector 1 Answer
Handles not displaying 1 Answer
C# unity custom editor multiple different components but same base class 2 Answers