Search Unity

Serialization Confusion

Discussion in 'Documentation' started by sjml, Oct 12, 2017.

  1. sjml

    sjml

    Joined:
    Feb 17, 2017
    Posts:
    2
    Hey folks!

    I had a whole lot of confusion this week over Serialization -- I was following the examples of how to save/load data in Unity, which point to BinaryFormatter as the way to go. (Specifically this video, but lots of resources around the net give the same technique.)

    Where I ran into trouble is where I wanted to do some custom serialization/deserialization stuff, and found my way to ISerializationCallbackReceiver. What took me a while to figure out was that this only applied to serialization being done by Unity, where BinaryFormatter is part of the System namespace. I was wondering if maybe it was because I was running the serialization on a separate thread, if the [Serializable] attributes were applied correctly, etc. Went down a lot of rabbit holes before I finally realized that BinaryFormatter doesn't go through the Unity serialization pipeline.

    Now that I understand it, it's obvious, of course, but it may be worth adding a note on the ISerializationCallbackReceiver documentation that it won't kick in when calling Serialize or Deserialize with BinaryFormatter.

    That's all! Thanks for the great work.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Note that there's nothing preventing you from using ISerializationCallbackReceiver with custom serialization formats. You just have to call the methods yourself.