Search Unity

BinaryFormatter Serialization Broken when change form .net 3.5 to .net 4.x

Discussion in 'Editor & General Support' started by Oscar-Tsang, Feb 4, 2019.

  1. Oscar-Tsang

    Oscar-Tsang

    Joined:
    Nov 7, 2012
    Posts:
    84
    Unity version 2018.3.4f1

    The save file is saved by Scripting Runtime Version .NET 3.5. But if switch the Scripting Runtime Version .NET 4.x, the file cannot loaded. It detects the format is not correct.

    What I found, using BinaryFormatter Serialization to save data, use Scripting Runtime Version .NET 3.5 can only open .NET 3.5 file, while using .NET 4.x only can open .NET 4.x Serialization file.

    When using hex editor to open the two files. There is many different. The main is .net 3.5 file the opening is "Assembly-CSharp", but the .net 4.x file it opening is "FAssembly-CSharp"

    I have a main problem, I am using serialization to save player game data, how do I upgrade the runtime to .net 4.x, but able to deserialize the .net 3.5 file?

    Unity is deprecated .net 3.5, we need to find the way to read the serialization file when upgrade to .net 4.x
     
  2. Oscar-Tsang

    Oscar-Tsang

    Joined:
    Nov 7, 2012
    Posts:
    84
    No one find it is a problem?
     
  3. backwheelbates

    backwheelbates

    Joined:
    Jan 14, 2014
    Posts:
    232
    It's breaking for me too. Anyone else find a solution to this?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If this were me, I'd implement my own save format that doesn't require the BinaryFormatter to be set in stone forever. Release a version of your game which reads the BinaryFormatter version of your save files, and re-saves them in your own format under your control. Then you can release later updates to your game using .net 4.x which will be able to read your save files.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Joe-Censored likes this.
  6. backwheelbates

    backwheelbates

    Joined:
    Jan 14, 2014
    Posts:
    232
    Thanks @Kurt-Dekker,
    For sure, Id like to steer clear of it. Although I have existing players where their data is stored in this way (I wish I knew better at the time). As the .NET version compatibility changed with Unity, I'm not even able to migrate the data into a better format.

    Is there anyway to salvage the older .NET data?

    Thanks again!!
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I don't know enough about binary formatter to answer this, but at the end of the day it's just bytes on the disk. If you can write yourself an old-style binary save (the way your game used to do), then grab and keep that data, it may be possible to coax a newer version into reading it.

    I have had to sunset old savegame formats before, and my upgrade logic is this:

    Code (csharp):
    1. if (NewFormatSaveGameExists)
    2. {
    3.   LoadNewFormat();
    4.   return;
    5. }
    6. else
    7. {
    8.   if (OldFormatSaveGameExists)
    9.   {
    10.     LoadOldFormat();
    11.     SaveNewFormat();
    12.     DeleteOldFormat();
    13.     return;
    14.   }
    15.   else
    16.   {
    17. //// must be a brand-new player!
    18.      return;
    19.   }
    20. }