I have a custom editor for a script. It serializes the data it uses. Part of this custom editor requires that it find and then modify values on some OTHER instance(s) of the object. i.e. the scene has multiple objects and the custom editor edits the currently selected one, but indirectly may modify some others.
I see I can use serializedObject.ApplyModifiedProperties() on the currently edited object, from the editor script, but how do I get the other object to also re-serialize i.e. SAVE the changes I made to some of its values? e.g. I directly modify some float variables e.g. t.p1=15; where t is the other object. This doesn’t get serialized on the other object without going into that object’s custom inspector and changing something. So if I then hit play, all my indirect changes are lost.
Using EditorUtility.SetDirty() fixes it because this lets you set an ‘there’ object dirty which causes a serialization. But is there a way to cause some other object to reserialize itself and save the changes I made to it indirectly, given it’s editor is not currently active?
Try to use serialized objects for the whole process.
If you have a reference to the object, just create a new serialized object, change it, then apply modified properties to it. Creating a serialized object isn’t free, but it seems pretty robust.
var otherSerializedObj = new SerializedObject(otherObject);
otherSerializedObj.FindProperlyRelative("p1").floatValue = 15;
otherSerializedObj.ApplyModifiedProperties();
hmm ok thanks, but wouldn’t this interfere with the fact that that external object is also going to have its own serialization going on once I activate it and editing some data? Why would you want two sets of serialization going on? Isn’t there some way to trigger that object to update itself?
Here I work directly with MonoBehaviours most of the time, outside of the above method snippet. Then I create a serialized object and modify it when I need to store or change some information about the specific monobehaviour that I’m working with.