Search Unity

File save and load error

Discussion in 'Scripting' started by Livindaki, Aug 6, 2019.

  1. Livindaki

    Livindaki

    Joined:
    Jun 13, 2017
    Posts:
    49
    i had same project run under unity 5 . and had no errors
    after upgrading to unity 2019 cause i buy an asset that need to run under unity 2019 i had error in my project after run this was most important error with loading and saving file :
    the error:
    Type 'Photon.Realtime.RoomInfo' in Assembly 'PhotonRealtime, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
    System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers (System.RuntimeType type) (at <7d97106330684add86d080ecf65bfe69>:0)
    System.Runtime.Serialization.FormatterServices+<>c__DisplayClass9_0.<GetSerializableMembers>b__0 (System.Runtime.Serialization.MemberHolder _) (at.....

    my code :
    Code (CSharp):
    1.  
    2.  public void Load_Room()
    3.     {
    4.         File_Were_Loaded = true;
    5.         if (File.Exists(Application.persistentDataPath + "/GN3.dat"))
    6.         {
    7.             Debug.Log("1");
    8.             BinaryFormatter bf = new BinaryFormatter();
    9.             Debug.Log("2");
    10.             FileStream file = File.Open(Application.persistentDataPath + "/GN3.dat", FileMode.Open);
    11.             Debug.Log("3");
    12.             if (file != null)
    13.             {
    14.                 First_Connection.MC = (My_Class)bf.Deserialize(file);
    15.                 Debug.Log("4");
    16.                 file.Close();
    17.                 Debug.Log("5");
    18.                 Fill_Rooms();
    19.                 Debug.Log("Rooms Were Loaded Successfuly !");
    20.                 //PhotonNetwork.JoinLobby(TypedLobby.Default);
    21.             }
    22.         }
    23.         else
    24.         {          
    25.             Go_To_Rooms_Screen();
    26.         }
    27.     }
    28. public static void Save()
    29.     {
    30.         BinaryFormatter bf = new BinaryFormatter();
    31.         FileStream file = File.Create(Application.persistentDataPath + "/GN3.dat");
    32.         bf.Serialize(file, MC);
    33.         file.Close();
    34.     }
    35. //my Saved Classes :
    36. using Photon.Realtime;
    37. using System;
    38. using System.Collections.Generic;
    39. using UnityEngine;
    40.  
    41. [Serializable]
    42. public class My_Class
    43. {
    44.     public Last_Join_Info last_join_info;
    45.     public List<_RoomsList> Rooms;
    46.     //public Player player;
    47.     public Player_Information PI;
    48.     public List<RoomInfo> rooms;
    49.     public string[] Password_For_Rooms=new string[50];
    50.     public bool Joined_RG5 ;
    51. }
    52. [Serializable]
    53. public class Last_Join_Info
    54. {
    55.     public string Room_Name="";
    56.     public int Room_Size=0;
    57.     //public string Player_Name="";
    58.     public bool Had_Enter_A_Room = false;
    59. }
    60.  
    61. [Serializable]
    62. public class _RoomsList
    63. {
    64.     public string RoomName;
    65.     public string RoomPassword;
    66.     public string RoomConfirmCode;
    67.     public int RoomSize;
    68.     public int Room_Coast_Per_one;
    69.     public int Room_Secret_Number;
    70.     public int Room_Ws_Deleted=0;
    71.  
    72.     //public byte[] Serialize()
    73.     //{
    74.     //    Debug.Log("1");
    75.     //    byte[] roomNameBytes = RoomHolderAndSnderThingy.EncodeString(RoomName);
    76.     //    byte[] passwordBytes = RoomHolderAndSnderThingy.EncodeString(RoomPassword);
    77.     //    byte[] confirmBytes = RoomHolderAndSnderThingy.EncodeString(RoomConfirmCode);
    78.     //    byte[] serialized = new byte[roomNameBytes.Length + passwordBytes.Length+ confirmBytes.Length + 16];
    79.     //    Debug.Log("2");
    80.     //    int location = 0;
    81.     //    Array.Copy(roomNameBytes, 0, serialized, location, roomNameBytes.Length);
    82.     //    location += roomNameBytes.Length;
    83.     //    Array.Copy(passwordBytes, 0, serialized, location, passwordBytes.Length);
    84.     //    location += passwordBytes.Length;
    85.     //    Array.Copy(confirmBytes, 0, serialized, location, confirmBytes.Length);
    86.     //    location += confirmBytes.Length;
    87.     //    Array.Copy(BitConverter.GetBytes(RoomSize), 0, serialized, location, 4);      
    88.     //    Array.Copy(BitConverter.GetBytes(Room_Coast_Per_one), 0, serialized, location, 4);
    89.     //    Array.Copy(BitConverter.GetBytes(Room_Secret_Number), 0, serialized, location, 4);
    90.     //    Array.Copy(BitConverter.GetBytes(Room_Ws_Deleted), 0, serialized, location, 4);
    91.     //    location += 4;
    92.  
    93.     //    return serialized;
    94.     //}
    95.  
    96.     //public int Deserialize(byte[] serialized, int startLocation)
    97.     //{
    98.     //    int location = startLocation;
    99.     //    int lengthOfString = 0;
    100.     //    Debug.Log("3");
    101.     //    RoomName = RoomHolderAndSnderThingy.DecodeString(serialized, out lengthOfString, location);
    102.     //    location += lengthOfString;
    103.     //    RoomPassword = RoomHolderAndSnderThingy.DecodeString(serialized, out lengthOfString, location);
    104.     //    location += lengthOfString;
    105.     //    RoomConfirmCode = RoomHolderAndSnderThingy.DecodeString(serialized, out lengthOfString, location);
    106.     //    location += lengthOfString;
    107.     //    RoomSize = BitConverter.ToInt32(serialized, location);
    108.     //    Room_Coast_Per_one = BitConverter.ToInt32(serialized, location);
    109.     //    Room_Secret_Number = BitConverter.ToInt32(serialized, location);
    110.     //    Room_Ws_Deleted = BitConverter.ToInt32(serialized, location);
    111.     //    location += 4;
    112.        
    113.  
    114.     //    return location - startLocation;
    115.     //}
    116.  
    117. }
    118. [Serializable]
    119. public class Player_Information
    120. {
    121.     public string Player_Name;
    122.     public string Password_Entered_For_Last_Room;
    123.     public string Last_Room_Joined_Name;
    124.     public bool Player_Is_Active;
    125.     public int Player_Secret_Number;
    126. }
    127.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    I'm not positive this will have any impact, but try the generic version for the above, if it exists:

    Code (csharp):
    1. First_Connection.MC = bf.Deserialize<My_Class>(file);
    Caution: I have no idea if it exists, but give it a try.
     
  3. Livindaki

    Livindaki

    Joined:
    Jun 13, 2017
    Posts:
    49
    no ... i had try that .. but my code hade 0 errors on unity 5 .
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Alas the world does change over time, especially software. :)

    You may need to upgrade the module that contains that 'Photon.Realtime.RoomInfo' class.

    Alternately if you have the source for that class, you might be able to add extra decoration (such as Serializable) to make the binary formatter happy. Or there might be other hints you can give the binary formatter, but I haven't used that construct at all myself.

    Might just need some more googling: it is almost a certainty that you're not the first person to encounter this exact error.