I am making a custom editor for one of my objects. A highly simplified version of that object looks like this:
public class MyClass : MonoBehaviour {
public int valueA;
public int valueB;
// Whether we want to use value A or B
public bool useA;
}
When useA is true, valueB is irrelevant and does not need to be shown in the inspector. This is why the custom editor class looks like this:
[CustomEditor(typeof(MyClass)), CanEditMulitpleObjects]
public class MyClassEditor : Editor {
public override void OnInspectorGUI() {
SerializedObject tg = new SerializedObject(target);
bool useA = tg.FindProperty("useA");
useA.boolValue = EditorGUILayout.Toggle("Use A", useA.boolValue);
if(useA.boolValue) {
int valueA = tg.FindProperty("valueA");
valueA.intValue = EditorGUILayout.Toggle("Value A", valueA.intValue);
} else {
// Do the same for valueB
}
tg.ApplyModifiedProperties();
}
}
Now, I am completely new to SerializedObjects
, so this might be horribly ineffecient (if so, please tell me!). That is however not the issue. When you have a lot of values to be set in the inspector, the code for the custom editor becomes pretty convoluted and is a lot of work to maintain, as you have to do FindProperty()
first, then set it using EditorGUILayout
, etc. So my question is: is there an easier way of obtaining values from a SerializedObject
? To write this code “cleaner”?