- Home /
Easy way to set multiple SerializedProperties
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"?
Answer by vexe · Feb 20, 2014 at 11:45 AM
Editor codes tend to get quite messy and long, that's why it's recommended to have a GUIHelper type of library to manage your often-used stuff, makes life simpler, much less code.
AFAIK, there's no way around FindProperty
- You could use EditorGUILayout.PropertyField
instead of manually using Toggles, etc. Using PropertyField
, it will automatically detect the type of the property, and it will handle undo/redo for you (your current code doesn't!) - Also, you don't need to create a new SerializedObject
, there's already a reference to the SerializedObject
of your target which is serializedObject
. And finally, that code shouldn't compile, FindProperty returns a SerializedProperty
, and you're assigning that to an int.
So here's the cut version of your code:
[CustomEditor(typeof(MyClass)), CanEditMulitpleObjects]
public class MyClassEditor : Editor {
public override void OnInspectorGUI() {
serializedObject.Update(); // don't forget this
SerializedProperty useA = serializedObject.FindProperty("useA");
EditorGUILayout.PropertyField(useA);
if(useA.boolValue) {
SerializedProperty valueA = serializedObject.FindProperty("valueA");
EditorGUILayout.PropertyField(valueA);
} else {
// Do the same for valueB
}
serializedObject.ApplyModifiedProperties(); // nor this
}
}
If you want, you could make an InitSp
method to initialize your serialized properties and make your gui code a little nicer, to do this you have to have them as member fields.
Your answer
Follow this Question
Related Questions
How do i stop my Editor window values from reseting? 0 Answers
Custom Inspector: Using SeralizedProperty changes the prefab values! 0 Answers
How to change inspector with non-Monobehaviour objects ? 1 Answer
Changing Inspector's Serialized Property Label (With Code Sample) 1 Answer
Select custom object from a list via PopUp in custom editor for ScriptableObject 0 Answers