Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Cannot Load My Serialized Buildings Anymore Saved With Previous Unity Version

Discussion in '2019.1 Beta' started by Firlefanz73, Apr 13, 2019.

  1. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    Hello,

    I downloaded the Release candidate 2019.1.0.f1 and most Things work very good!

    But I have one Problem with loading my Buildings.

    Before this Code worked perfect, now it throws Errors:

    Code (CSharp):
    1.     public static Globals.BuildingStruct LoadBuilding(string name)
    2.     {
    3.         //Debug.Log ("Load Building " + name.ToString ());
    4.         try
    5.         {
    6.             Globals.BuildingStruct building = new Globals.BuildingStruct();
    7.             Stream stream;
    8.  
    9.             TextAsset asset = Resources.Load("Buildings/" + name) as TextAsset;
    10.             stream = new MemoryStream(asset.bytes);
    11.  
    12.             BinaryFormatter reader = new BinaryFormatter();
    13.  
    14.             building.Version = (string)reader.Deserialize(stream);
    15.             building.Name = (string)reader.Deserialize(stream);
    As soon as it Comes to this Point: building.Version = (string)reader.Deserialize(stream);

    it says:
    System.Runtime.Serialization.SerializationException: No map for object '100663296'.
    at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObject () [0x0006c] in <1f0c1ef1ad524c38bbc5536809c46b48>:0


    Any idea how I can get my Buildings to load again? The stream has a length, so the files are there but seem to cannot deserialize…

    Thanks a lot. I can add the save Routine if it helps...
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    If the serialization changed you probably have to save the assets streams again with the new Unity version.
     
  3. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    That's not possible. These are manually builded houses with own Editor.
    That would be a really huge Problem, I would like to upgrade to Unity 2019 and that would be the knock out.
     
  4. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    maybe serializing the buildings from your editor with JSON to test. At least that would be easier to debug when deserializing
     
    Firlefanz73 likes this.
  5. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,620
    Unity 2019.1 uses .NET4.x Scripting Backend by default as far as I know. Perhaps your project was on, the now deprecated, .NET3.5 thingy before?
     
    Firlefanz73 likes this.
  6. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    No, it was on .Net4. But thanks a lot for trying to help me!
     
  7. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    Is there some Kind of Encoder I must set now that has changed from Unity 2018 to 2019?

    These are not really TextAssets, I only wanted to hide them away, so I renamed them to .txt and put them under Ressoruces. This is their structure:

    Code (CSharp):
    1.     [Serializable]
    2.     public struct BuildingStruct
    3.     {
    4.         public string Name;
    5.         public string Version;
    6.         public string Creator;
    7.         public Location location;
    8.         public int Width;
    9.         public int Height;
    10.         public int Depth;
    11.         public int xr;
    12.         public int yr;
    13.         public int zr;
    14.         public ushort[,,] Blocks;
    15.     }
    Maybe this is a Problem now in 2019?
     
    Last edited: Apr 13, 2019
  8. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,066
    BinaryFormatter is not intended for storing data, it's made for serializing data in RPC calls. Its default behavior is to store the assembly version together with the type information, which means it only allows to deserialize data that was created with the same assemblies. Don't expect data serialized with BinaryFormatter to deserialize when you update your Unity version.

    You can try setting the BinaryFormatter's AssemblyFormat to FormatterAssemblyStyle.Simple to ignore the assembly version. This needs to be set before serialization, you'll have to go back to the old Unity version and re-save the data.

    But a better option would be to use a more robust serialization format. You could use SerializedObjects, Unity's JsonUtility, or also BSON with Json.Net, if you are concerned about file size.
     
    LeonhardP likes this.
  9. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    Thanks for the Infos from you all!

    I wanted to save my Buildings and deliever them as a ressource.

    Code (CSharp):
    1.     [Serializable]
    2.     public struct BuildingStruct
    3.     {
    4.         public string Name;
    5.         public string Version;
    6.         public string Creator;
    7.         public Location location;
    8.         public int Width;
    9.         public int Height;
    10.         public int Depth;
    11.         public int xr;
    12.         public int yr;
    13.         public int zr;
    14.         public ushort[,,] Blocks;
    15.     }
    They are saved this way:

    Code (CSharp):
    1.             Stream stream = File.Open(Globals.DataPath + "/Buildings/" + building.Name + ".bib", FileMode.Create);
    2.             BinaryFormatter writer = new BinaryFormatter();
    3.             Debug.Log("Writing Information");
    4.             writer.Serialize(stream, building.Version);
    5.             writer.Serialize(stream, building.Name);
    6.             writer.Serialize(stream, building.Creator);
    7.             writer.Serialize(stream, building.location);
    8.             writer.Serialize(stream, building.Width);
    9.             writer.Serialize(stream, building.Height);
    10.             writer.Serialize(stream, building.Depth);
    11.             writer.Serialize(stream, xr);
    12.             writer.Serialize(stream, yr);
    13.             writer.Serialize(stream, zr);
    14.             Debug.Log("Writing Blocks");
    15.             writer.Serialize(stream, building.Blocks);
    16.  
    17.             stream.Close();
    18.  
    I have then renamed them to .txt and put under resources, so I can load them like a TextAsset…

    So I really Need to deinstall Unity 2019, reinstall 2018, write a Tool that loads all Buildings in the old way and saves them in a new way, Right? Is there now a way I can save my Buildings to resources now?

    Thanks again
     
    Last edited: Apr 13, 2019
  10. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    It is not the binaryformatter. It is something About loading it like a TextAsset, that worked in 2018 and not in 2019.

    if (File.Exists(Globals.DataPath + "/Buildings/" + name + ".bib"))
    {
    stream = File.Open(Globals.DataPath + "/Buildings/" + name + ".bib", FileMode.Open);
    }
    If I do this to a copy of the same file, it works! Replacing this:

    TextAsset asset = Resources.Load("Buildings/" + name) as TextAsset
    stream = new MemoryStream(asset.bytes)

    So I could add the renamed files to my Installation somehow, or is there an other way to load this as an asset from my Resources Folder?

    Thanks :)
     
    asreed1111 likes this.
  11. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    I renamed the fake resources files with my Buildings to ."bytes" instead of ".txt".

    Now it works like before with loading them from my resources.

    Thanks for the hints!