Test whether current stack frame is "during serialization" ?

Hi,

is there an easy and quick way to test in a script whether we are currently in a “serialization context”, i.e. not allowed to call Unity functions like .gameObject or GameObject.name? Something like Application.isDuringSerializationContext ?

Use case is, we have an own logging facility that tries to gather some extended context information. We want this logging function to be usable from basically anywhere, included OnAfterDeserialize callbacks.

But nowadays (5.4.0b1) we get errors complaining that “ToString is not allowed to be called during serialization, call it from Awake or Start instead.”

Edit: Is it guaranteed that the Unity main thread stays the same and all code on this thread is never in this “during serialization”? Then I might compare Thread.CurrentThread?

I don’t think there’s a concept of “serialization context”, but only of different threads (e.g: during serialization, the code probably executes on a different thread).

That’s the theory of course, i think i tried to do the comparison sometime in the past (whether the currently executing thread is the scripting thread, but it actually did not work out as i thought it would).