2
votes

I am having a hard time figuring out how to get my [SerializeField] private boolean variable to show in the inspector through my Editor I have created. I normally do something like :

myTarget.AutoFill = EditorGUILayout.Toggle(...)

if the variable was public but since I have it as 'private' do I lose the chance to use it in my Options_Manager_Editor? Is there a way around this since I want to keep Encapsulation? Would I have to change to using OnGUI, just make it public since another script is needed it or make a public method in my Options_Manager that has the code for my boolean AutoFill for customizing my inspector?

My Options_Manager code:

public class Options_Manager : MonoBehaviour {
    // Auto fill.
    [SerializeField] 
    private bool AutoFill;
}

My Options_Manager_Editor code:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEditor;

[CanEditMultipleObjects]
[CustomEditor(typeof(Options_Manager))]
public class Options_Manager_Editor : Editor {

    public override void OnInspectorGUI(){
        // Grab the script.
        Options_Manager myTarget = target as Options_Manager;
        // Set the indentLevel to 0 as default (no indent).
        EditorGUI.indentLevel = 0;
        // Update
        serializedObject.Update();

        EditorGUILayout.Toggle(new GUIContent("Test Fill", "Test Fill Tooltip."), serializedObject.FindProperty("AutoFill").boolValue);
        // Apply
        serializedObject.ApplyModifiedProperties();
    }
}

EDIT: I just recently tried something but I am not sure if this can cause any problems down the road but if anyone can look at this and let me know that would be great.

My Options_Manager code:

public class Options_Manager : MonoBehaviour {
    // Auto fill.
    [SerializeField] 
    private bool _autoFill;

public bool AutoFill{
    get{
        return _autoFill;
    }
    set{
        _autoFill = value;
    }
}
}

My Options_Manager_Editor code:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEditor;

[CanEditMultipleObjects]
[CustomEditor(typeof(Options_Manager))]
public class Options_Manager_Editor : Editor {

    public override void OnInspectorGUI(){
        // Grab the script.
        Options_Manager myTarget = target as Options_Manager;
        // Set the indentLevel to 0 as default (no indent).
        EditorGUI.indentLevel = 0;
        // Update
        serializedObject.Update();

        myTarget.AutoFill = EditorGUILayout.Toggle(new GUIContent("Auto Fill", "Test Tooltip"), serializedObject.FindProperty("_autoFill").boolValue);
        // Apply
        serializedObject.ApplyModifiedProperties();
    }
}
1
Related: note that void OnValidate() is an INCREDIBLY HANDY CALL which is almost undocumented by Unity. on the runtime side it's the only practical way to change something "during Play, if you change it in the inspector variable"Fattie
@JoeBlow I am not sure how that is going to help me in my situation. Can you explain based on my post how OnValidate will be helpful?JoeyL
It does not help you. Sorry, I just noted that there for the benefit of anyone googling this topic. (A good use for "comments".) Cheers!Fattie
Seen all caps bold style text in comments. Knew it would be @JoeBlowUmair M
LOL @UmairM :)Fattie

1 Answers

0
votes

MyProjectNamespace.cs

using UnityEngine;
using UnityEditor;

namespace MyProjectNamespace
{
    [ CustomEditor( typeof( MyMonoBehaviour ) ) ]

    [ CanEditMultipleObjects ]

    public class MyMonoBehaviourInspector : Editor
    {
        #region Variables

        SerializedProperty _myBoolSP;

        MyMonoBehaviour _myMonoBehaviour;

        #endregion Variables

        #region Functions

        void OnEnable()
        {

            _myMonoBehaviour = serializedObject.targetObject as MyMonoBehaviour;

            _myBoolSP = serializedObject.FindProperty( "_myClass._myBool" );

            Debug.Log( "_myBoolSP = " + _myBoolSP.boolValue );

        }



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

            _myBoolSP.boolValue = EditorGUILayout.Toggle( "A Boolean", _myBoolSP.boolValue );

            GUILayout.Label( _myMonoBehaviour.MyBoolValue() );

            serializedObject.ApplyModifiedProperties();
        }

        #endregion Functions

    }

}

MyMonoBehaviour.cs

using UnityEngine;

namespace MyProjectNamespace
{
    public class MyMonoBehaviour : MonoBehaviour
    {
        #region Variables

        [ SerializeField ] private MyClass _myClass;

        #endregion Variables

        #region Functions

        public string MyBoolValue()
        {
            return "The value of _myBool is " + _myClass.MyBool;
        }

        #endregion Functions

    }

}

MyClass.cs

using UnityEngine;

namespace MyProjectNamespace
{
    [ System.Serializable ]

    public class MyClass
    {
        #region Variables

        [ SerializeField ] private bool _myBool;

        #endregion Variables

        #region Functions

        public bool MyBool
        {
            get{ return _myBool; }
        }

        #endregion Functions

    }

}

For future reference. Note that you can have private variables, you just need to use [ SerializeField ], (and [ System.Serializable ] for classes that don't extend MonoBehaviour). You probably won't need using UnityEngine.UI for editor classes.