Home > database >  Display bool in inspector from Editor
Display bool in inspector from Editor

Time:01-28

I have a script that contains 2 boolean variables. I am displaying the first one. Now I want to display the second boolean variable only and only if the first boolean variable is false. I am using custom inspector, how do I do this?

public bool myFirstBool = true;

[HideInInspector]
public bool mySecondBool = false;

My Editor Script

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

[CustomEditor (typeof (ScriptMain))]

public class ScriptMainEditor : Editor {

    ScriptMain actualTarget;

    private void OnEnable () {

        actualTarget = (ScriptMain) target;

    }

    public override void OnInspectorGUI () {
        DrawDefaultInspector (); // for other non-HideInInspector fields

        if (actualTarget.myFirstBool == true)
        {
          //Show mySecondBool in Inspector
        } else {
          //Hide mySecondBool in Inspector
        }
        base.OnInspectorGUI ();
    }

}

CodePudding user response:

In a proper editor script you don't go through the target at all except you know exactly what you are doing!

This

  • doesn't mark changed fields and objects dirty
  • thus doesn't save those values
  • doesn't handle undo/redo
  • doesn't handle prefab overwrites

Rather go through the enter image description here

  •  Tags:  
  • Related