- Home /
CustomEditor & Serialization not keeping Overrides & not working with Animation
I'm trying to expose my class via a CustomEditor, but I'm running into a few roadblocks.
public class ZExample : MonoBehaviour
{
[SerializeField] public float mColor0Factor = 1;
}
[CustomEditor(typeof(ZExample))]
[CanEditMultipleObjects]
public class ZExampleEditor : Editor
{
SerializedProperty mColor0Factor;
public override void OnInspectorGUI()
{
serializedObject.Update(); //BEGIN
ZExample lZExample = (ZExample)target;
lZExample.mColor0Factor =
EditorGUILayout.Slider( lZExample.mColor0Factor, 0.0f, 2.0f );
serializedObject.ApplyModifiedProperties(); //END
}
}
//1 When adding a controller for example, it will revert to the prefab value once you hit play. So it only retains the prefab values, not the local override.
[SerializeField] public AnimatorController mController = null;
As far as I can tell, [System.Serializable] on the class and [SerializeField] on the properties don't do anything to help with this. Are these needed, or am I mixing custom editor & property drawers ?
//2 When keying "mColor0Factor" via animation, the Inspector GUI doesn't turn red and any changes made to the value via the Inspector GUI are NOT keyed and are overridden by the values already keyed.
This should suposedly be fixed by marking it as dirty on change, but again, this doesn't work on my end.
EditorGUI.BeginChangeCheck();
{
lZExample.mColor0Factor =
EditorGUILayout.Slider( lZExample.mColor0Factor, 0.0f, 2.0f );
}
if(EditorGUI.EndChangeCheck())
{ EditorUtility.SetDirty(target); }
//3 Is it usefull to get the Property from the class on initialisation and why ?
mColor0Factor = serializedObject.FindProperty("mColor0Factor");
Answer by Adam-Mechtley · Jun 14, 2017 at 08:55 AM
As you have no doubt surmised, the answers to all of these questions are related.
In many cases (e.g., simple types and UnityEngine.Object references), if you have a public field, there is no need for explicitly masking the field with SerializeFieldAttribute
. This attribute is only necessary for private fields. Respectively, adding System.SerializableAttribute
should only be necessary if you have a custom struct or class you need to serialize. You might find this page in the documentation helpful.
In general, whenever you do a custom editor, property drawer, or similar, you should get a handle to properties on the target object using SerializedProperty rather than reading directly from the target object. In your case, it needs to look something like this:
[CustomEditor(typeof(ZExample)), CanEditMultipleObjects]
public class ZExampleEditor : Editor {
SerializedProperty mColor0Factor;
void OnEnable() {
mColor0Factor = serializedObject.FindProperty("mColor0Factor");
}
public override void OnInspectorGUI() {
serializedObject.Update();
// note method override passing SerializedProperty directly
EditorGUILayout.Slider(mColor0Factor, 0f, 2f);
serializedObject.ApplyModifiedProperties();
}
}
When you use this approach, it will properly handle Undo/Redo, dirtying, animation, and so on. In most cases that's it—you're done!
When you must use some kind of control that cannot accept a SerializedProperty for some reason (or if you make your own control that cannot for some reason), you need to do 2 things:
Use Undo.RecordObject() right before you modify the object (so first thing inside of the
if (EditorGUI.EndChangeCheck())
block. This will dirty any modified fields so that you won't lose changes when entering play mode, and also makes the modification compatible with the Undo system.Enclose the control in calls to EditorGUI.BeginProperty() and EditorGUI.EndProperty(). Per the documentation, these will handle styling and context menus for indicating property dirtiness, reverting to prefab values and so on. What's not as explicit however, is that they are also responsible for the color styling applied when the properties are animated.
Your answer
Follow this Question
Related Questions
Changing Inspector's Serialized Property Label (With Code Sample) 1 Answer
How to get fields of a custom script through SerializedProperty.FindPropertyRelative? 1 Answer
Draw custom Property Field in Custom Editor 1 Answer
Replicate "Apply" and "Revert" button functionality in ScriptableObject Editor Inspector 2 Answers