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();
}
}
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