Hide Variable Depending on Bool

Hey,
There are many posts on this and a couple with full scripts. However, one I tried that used an attribute just would not work. (Hiding or Disabling inspector properties using PropertyDrawers within Unity - Brecht Lecluyse) This one would give me a type or namespace error for the ConditionalHideAttribute class no matter where I positioned the scripts. (Editor, not editor, one in editor, none in editor).

What I have at the minute is:

using UnityEditor;

[CustomEditor(typeof(CameraMovement))]
public class CameraMovementEditor : Editor
{
    public override void OnInspectorGUI()
    {
        //base.OnInspectorGUI();

        CameraMovement cam = (CameraMovement)target;

        cam.edgePan = EditorGUILayout.Toggle("Edge Pan", cam.edgePan);

        if (cam.edgePan == true)
        {
          // Enable distFromEdge field 
          // Enable panSpeed field 
        }
    }
}

The issue I am having is someone said on another persons post “if it is true, enable the fields”. But, how?
All / any help would be appreciated.
Thanks.

Something along these lines?

1 Like

Yeah haha. I abandoned that idea now and went with something simple and split my variables into classes. Nice and simple. Useful link though.

1 Like

Something like this could work, if you happen to have this need again:

#if UNITY_EDITOR

[CustomEditor(typeof(CameraMovement))]
public class CameraMovementEditor : Editor
{
    private CameraMovement _cam;
    private SerializedProperty _checkField;
    private SerializedProperty[] _lockedFields;


    private void OnEnable()
    {
        _cam = (CameraMovement) target;

        //nameof is used to facilitate refactoring (must be either the fields public or this class inside CameraMovement)
        _checkField = serializedObject.FindProperty(nameof(_cam.edgePan));

        _lockedFields = new[]
        {
            serializedObject.FindProperty(nameof(_cam.distFromEdge)),
            serializedObject.FindProperty(nameof(_cam.panSpeed))
        };
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        GUI.enabled = true;
        //the bool field has to be shown
        EditorGUILayout.PropertyField(_checkField);

        GUI.enabled = _checkField.boolValue;    // if you want to see them but have them locked

        foreach (var field in _lockedFields)
        {
            // if (_checkField.boolValue)          //if you want to hide them completely
                EditorGUILayout.PropertyField(field);
        }

        GUI.enabled = true;

        serializedObject.ApplyModifiedProperties();
    }
}
#endif