Unity: how to let my variables show up in the inspector if a boolean is true, and hide it when the boolean is false

Viewed 291
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyScript: MonoBehaviour
{
    public bool a;
    int b;
}

for example, this is my script, and I want to show a in the inspector if b is true, and hide a in the inspector when b is false.

How to make my custom editor?

1 Answers

Add a new CustomEditor script as follows:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(MyScript))]
public class MyScriptEditor: Editor
{
    public override void OnInspectorGUI() 
    {
        // Call normal GUI (displaying "a" and any other variables you might have)
        base.OnInspectorGUI();

        // Reference the variables in the script
        MyScript script = (MyScript)target;

        if (script.a) 
        {
            // Ensure the label and the value are on the same line
            EditorGUILayout.BeginHorizontal();

            // A label that says "b" (change b to B if you want it uppercase like default) and restricts its length.
            EditorGUILayout.LabelField("b", GUILayout.MaxWidth(50));
            // You can change 50 to any other value

            // Show and save the value of b
            script.b = EditorGUILayout.IntField(script.b);
            // If you would like to restrict the length of the int field, replace the above line with this one:
            // script.b = EditorGUILayout.IntField(script.b, GUILayout.MaxWidth(50)); // (or any other value other than 50)

            EditorGUILayout.EndHorizontal();
        }
    }

}

You need to change the definition of the variable b in the MyScript class, like this:

// Hide b by default, but make it public so MyScriptEditor can access it
[HideInInspector]
public int b;

If you prefer, you can condense everything into your MyScript class, as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class MyScript: MonoBehaviour
{
    public bool a;
    
    //Hide b by default, but make it public so MyScriptEditor can access it
    [HideInInspector]
    public int b;

    [CustomEditor(typeof(MyScript))]
    public class MyScriptEditor: Editor
    {
        public override void OnInspectorGUI() 
        {
            // Call normal GUI (displaying "a" and any other variables you might have)
            base.OnInspectorGUI();

            // Reference the variables in the script
            MyScript script = (MyScript)target;

            if (script.a) 
            {
                // Ensure the label and the value are on the same line
                EditorGUILayout.BeginHorizontal();

                // A label that says "b" (change b to B if you want it uppercase like default) and restrict its length.
                // You can change 50 to any other value
                EditorGUILayout.LabelField("b", GUILayout.MaxWidth(50));

                // Show and save the value of b
                script.b = EditorGUILayout.IntField(script.b);
                // If you would like to restrict the length of the int field, replace the above line with this one:
                // script.b = EditorGUILayout.IntField(script.b, GUILayout.MaxWidth(50)); // (or any other value other than 50)

                EditorGUILayout.EndHorizontal();
            }
        }
    }
}

I hope this helps your program's needs, and let me know if you need any more help!

Related