Please clarify ISerializationCallbackReceiver

Hello,

Apologies if this has been answered before, but I can’t seem to find the answer. The official documentation isn’t very clear (Unity - Scripting API: ISerializationCallbackReceiver.OnBeforeSerialize). I can understand that I should not depend on the order of serialization or deserialization in between objects.

However, this leaves me with an unanswered question: Are all OnAfterDeserialize calls guaranteed to happen after all OnBeforeSerialize calls? Of course I know that it will happen for a single object, but what about other objects? For example, in hot-reload, can I safely assume that in the OnAfterDeserialize() method of object A, the OnBeforeSerialize() method of object B was already called?

Thank you.

To answer your question it really all depends on what unity is trying to do. For example when you open up the editor for the first time unity needs to load all the serialize data that you have in your project, so in this case only OnAfterDeserialize gets called. There is no need to call the OnBeforeSerialize function because nothing is being saved.

If you modify and save a change to a ScriptableObject, then only OnBeforeSerialize gets called because unity is only saving data and nothing is being loaded. However, if you undo an action in the editor, then unity needs to load a previously saved(serialized) state but at the same time it needs to remember the changes in order to be able to perform a redo, so in this case OnBeforeSerialize gets called before OnAfterDeserialize. Let me know if that helps.

1 Like

Thank you for your reply. Yes, I understand that, and it makes sense that there are times when only one method is called, but from my understanding serialization always happens before; i.e. in order for deserialization to happen, serialization has, at some point in the past, been called.

My question is mostly on editor hot-reload after changing something. During this time, everything is serialized and then deserialized. My question is: After hot-reload, during OnAfterDeserialize on object A, has OnBeforeSerialize in object B already been called? Or is it possible that the order might not be guaranteed?

Example:
Is this order of execution possible?
ObjectA.OnBeforeSerialize()
ObjectA is serialized and deserialized
ObjectA.OnAfterDeserialize();
ObjectB.OnBeforeSerialize();
ObjectB is serialized
ObjectC.OnBeforeSerialize();
ObjectC is serialized
ObjectC.OnAfterDeserialize();
ObjectC is deserialized
ObjectB.OnAfterDeserialize();
ObjectB is deserialized

Or is it always guaranteed to be like this:
All OnBeforeSerialize methods
All OnAfterSerialize methods

It makes sense that it is, but I just want to make sure because I wrote an editor tool that kind of depends on it.

I see what you’re saying. I found a __post __on the unity blog where they talk about hot reloading of the editor; however, I been using ISerializationCallbackReceiver for a few months now in one of my plugins for custom events and for me the execution is not consistent.

Edit: I did read that post in the past, but I’d missed this part. Looking at it again, it looks like it works as desired. It guarantees all OnBeforeSerialize methods are called before all OnAfterDeserialize methods. The exact order doesn’t matter to me.

Thank you! :smile: