1
\$\begingroup\$

I'm making a node editor in Unity using an EditorWindow, I'm currently trying to make it so designers can add their own methods to a node using serialized UnityEvents; but I haven't been able to get it to function properly.

SerializedObject serializedObject = new SerializedObject(selectedNode.NodeState);

SerializedProperty property = serializedObject.GetIterator();
property.Next(true);

while (property.NextVisible(false))
{
    EditorGUILayout.PropertyField(property);

}
serializedObject.ApplyModifiedProperties();

serializedObject.Update();

The above code runs through all necessary fields of the class so the user can modify them.

Everything functions perfectly with the exception of UnityEvents. They almost do as I intend them to do, except they don't preserve the variable inputs of inserted object's method. If I keep working within the editor window they stay, but if I close the window and re-open the inputted variable resets.

I thought this may be caused by an issue somewhere else in my code, but all other fields work fine.

\$\endgroup\$
1
\$\begingroup\$

I think the problem is that all actions in the custom Editor or EditorWindow you should do between two calls:

serializedObject.Update()

// Here is some of your code

serializedObject.ApplyModifiedProperties()

But in your code these lines goes below the main code. Moreover, the sequence is wrong.

I think the right way will be:

serializedObject.Update()

while (property.NextVisible(false)) {
    EditorGUILayout.PropertyField(property);    
}

serializedObject.ApplyModifiedProperties()
\$\endgroup\$
1
  • \$\begingroup\$ Doesn't seem to fix the issue, unfortunately. I believe the update function updates the UI property to show the current saved data, where as ApplyModifiedProperties saves the user inputted data. \$\endgroup\$ Jun 24 '17 at 10:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .