How acess private variable inside Custom editor, and interact with it on inspector.

Hi, guys anyone know’s how to do like this:

137959-screenshot-1.png

but with private variables?
Basically what i want is to set private variables that i could get from hierarchy.

Here the codes that i’m using.

[System.Serializable]
public class ButtonScript : Button
{
    public GameObject m_Obj;
}

And

[ CustomEditor ( typeof(ButtonScript) ), CanEditMultipleObjects ]
public class Editor_Button : ButtonEditor
{
    public override void OnInspectorGUI ( )
    {
        base.OnInspectorGUI (  );

        var content = (ButtonScript)target;

        GUILayout.Label("Custom Extend", EditorStyles.boldLabel);

        content.m_Obj = (GameObject)EditorGUILayout.ObjectField("Game Object",content.m_Obj, typeof(GameObject), true);
    }
}

Solution

Found the Solution for anyone look for it.


Class:

    public class ButtonScript : Button
    {
        [SerializeField] private GameObject m_Obj;
        [SerializeField] private protected Gameobject m_Obj2; // this is just to example.
    }

Editor:

    [ CustomEditor ( typeof(ButtonScript) ), CanEditMultipleObjects ]
    public class Editor_Button : ButtonEditor
    {
         private   SerializedProperty myGameObject;
         private   SerializedProperty myGameObject2;

    protected virtual void OnEnable ( )
    {
        myGameObject         = serializedObject.FindProperty ( "m_Obj" );
        myGameObject2      = serializedObject.FindProperty ( "m_Obj2" );
    }

        public override void OnInspectorGUI ( )
        {

           base.OnInspectorGUI();

            serializedObject.Update (  );

            DrawComponents (  );

            serializedObject.ApplyModifiedProperties (   );
        }
    protected virtual void DrawComponents()
    {
        EditorGUILayout.ObjectField(myGameObject, new GUIContent("My Game Object", "My GameObject Description"));
        EditorGUILayout.ObjectField(myGameObject2, new GUIContent("My Game Object 2", "My GameObject 2 Description"));
    }

    }