Custom editor window + UnityEvent, SerializedObject troubles.

So, I’ve got this in a custom editor window:

        if (actionViewsExpanded[drawingState])
        {
            SerializedObject serializedObject = new SerializedObject(drawingState);
            SerializedProperty onEnter = serializedObject.FindProperty("onEnterAction");
            EditorGUILayout.PropertyField(onEnter);
            SerializedProperty onUpdate = serializedObject.FindProperty("onUpdateAction");
            EditorGUILayout.PropertyField(onUpdate);
            SerializedProperty onExit = serializedObject.FindProperty("onExitAction");
            EditorGUILayout.PropertyField(onExit);
            if (GUI.changed) serializedObject.ApplyModifiedProperties();
        }

Superficially, it appears to work. However, the events don’t seem to actually get modified. These are modifying a field on a MonoBehaviour on a prefab, and changing these does not modify the values when I look at the prefab in the Inspector. Also, modifying the inspector only sometimes seems to change what I see in the custom editor.

What it seems like is, the first time I call new SerializedObject(drawingState), it seems to make a copy of drawingState that it uses in perpetuity. When I first open it, it copies whatever values have been set in the inspector. However, from that point on, they’re modifying two completely different objects.

Anyone come across this odd behavior of the SerializedObject class? How do I force it to completely refresh every time? And, why does ApplyModifiedProperties seem to do nothing?

Hey there,

This most likely has to do with the fact that you are creating a new Serialized Object every call of OnGUI (Which gets called once for every type of event on that frame). So when GUI.Changed is true it’s on a different event then when the data really got modified. So it just applies the changes (which there are none) back to the object.

I might be wrong but just quickly overlooking this that seems like it might be a problem.

You could also try to just not do if the if check and see if your changes gets applied.

Cheers,
Byron

Hey,

I am looking into adding UnityEvents to a custom editorwindow but I can’t seem to understand where the SerializedObject is being created from. What is this ‘drawingState’ referring to?

Thanks,
jrDev

[edit: Sorry, I just noticed that this thread is a bit old.]

Creating a new SerializedObject every time should be fine. Have you tried adding serializedObject.Update() after creating the new SerializedObject?

I don’t think you need to pass true for EditorGUILayout.PropertyField’s includeChildren parameter, but have you tried that? (I’m out of the office at the moment and can’t test these.)

@crafTDev - drawingState looks to be a variable that’s specific to StarManta’s script. Provide your object instead. For example, if your script is:

public class MyScript : MonoBehaviour {
    public UnityEvent myEvent;
}

Then use something like:

new SerializedObject((target as MyScript).myEvent);

(You should do more error checking than that; it’s just an example.)