Search Unity

ISerializationCallbackReceiver woes

Discussion in 'Scripting' started by silentslack, Jun 11, 2021.

  1. silentslack

    silentslack

    Joined:
    Apr 5, 2013
    Posts:
    393
    Hi,

    This is driving me nuts. Here is the most basic example to illustrate my problem.

    I have a ScriptableObject saved as an asset called SB_SceneAsset:



    It holds a reference to a custom class SB_SceneData:



    This holds a single integer field. I want to serialize the data in SB_SceneData before reapplying after deserialize. Should be simple but I get this:

    bb3b0cbb4fb4096f62974768ef467375.gif

    It seems SceneData doesn't have any modified data inside it. Why? There is no custom inspector code running.

    How can I tell it to Serialize before SB_SceneAsset :mad:
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,000
    Uhm, the result is exactly what you would expect. Why do you use the serialization callbacks in the first place if the data you want to serialize is serializable by default? It seems you don't really understand what the interface is good for ^^. The point of that interface is whenever the class is serialized OnBeforeSerialize will be executed which can process / transform any data that is not serializable and copy it into fields that are serialized so it will be picked up by the serializer just normally.

    On the other hand, whenever the class is deserialized OnAfterDeserialize will be executed to reverse the above. So after the serializable data has been deserialized by Unity, this callback can now restore the data that originally wasn't serializable.

    In your case your SB_SceneData is already a supported serializable class, so it's serialized by default. You now copy the value into another field and copy it back. Therefore the serialized value in the actual field inside your SB_SceneData class becomes pointless since every time when the class is deserialized, you replace that value with the value stored in _SerializedData.

    Note: The inspector can only show serialized data. Whenever the inspector needs to be redrawn, your class is serialized and derialized again. Whenever you change anything in the inspector, you actually change the serialized data which is then deserialized into your actual object. Since your OnAfterDeserialize will overwrite yourID field with the value serialized in your _SerializedData field, any direct changes to your ID field through the inspector would be pointless.
     
  3. silentslack

    silentslack

    Joined:
    Apr 5, 2013
    Posts:
    393
    Hi, yes I understand the point of the ISerializationCallback and why you would use it, I wanted to show a very basic example of storing and restoring some data. In my actual example, I'm using FullSerializer to serialize a custom class that contains a Dictionary etc.

    However, I see what you're saying. The default Inspector modifications aren't picked up in OnBeforeSerialize function as the default inspector uses serialized data that have yet to be serialized. That makes sense and the bit I was missing, thank you!