Utilizing CustomEditor without exposing values - is it possible?

This simple script does the following: if bool field is true, int field appears in the inspector. If false, it doesn’t.

It works, but I’m worried about both fields being public. I don’t want them to be changed from anywhere but CustomEditor script. Especially I don’t want them to be publicly writable

Is there a way to do it?

using UnityEditor;
using UnityEngine;

public class Test 
{
    public bool TestBool;
    public int TestInt;
}

[CustomEditor(typeof(Test))]
public class Test_Editor : Editor
{
    public override void OnInspectorGUI()
    {
        var script = (Test)target;

        script.TestBool = EditorGUILayout.Toggle("TestBool: ", script.TestBool);

        if (script.TestBool)
            return;

        script.TestInt = EditorGUILayout.IntField("TestInt: ", script.TestInt);
    }
}

There are several good answers already I guess. But something like this using serialized data:

First - here is a MonoBehaviour class with a private field.

// Your MB class
public class CustomScript : MonoBehaviour
{
    [SerializeField] private int myValue;
}

Then your Editor for your MB class:

[CustomEditor(typeof(CustomScript))]
public class CustomScriptEditor : Editor
{
    public override void OnInspectorGUI()
    {
        // Use serialized data to get your value
        serializedObject.Update();

        EditorGUILayout.LabelField("Serialized value:");
        
        // Find the property/value
        var prop = serializedObject.FindProperty("myValue");

        // Draw it using property field
        EditorGUILayout.PropertyField(prop);

        // Apply changes
        serializedObject.ApplyModifiedProperties();
    }
}

Check some anwers by Bunny83 and others that know this topic really well, here is one link: Expose Private Field in Custom Editor