Search Unity

Data Serializer iOS JIT Error

Discussion in 'Scripting' started by KeiseEntertainment, Sep 1, 2015.

  1. KeiseEntertainment

    KeiseEntertainment

    Joined:
    May 5, 2015
    Posts:
    23
    Hi, my name is Felipe and i'm getting an error using BinarySerialzer and other things like that. I changed all my save system to be more secure and to serialize it to do a cloud save on Android and worked fine, but for iOS do not work.

    I followed this unity live training: http://unity3d.com/pt/learn/tutoria...ining-archive/persistence-data-saving-loading

    And already try this: http://answers.unity3d.com/questions/30930/why-did-my-binaryserialzer-stop-working.html

    My Player Data Controll script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.Runtime.Serialization.Formatters.Binary;
    5. using System.IO;
    6.  
    7. public class PlayerDataControll : MonoBehaviour {
    8.  
    9.     public static PlayerDataControll playerData;
    10.  
    11.     public int gold = 0;
    12.     public int totalGold = 0;
    13.     public int[] stencils = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    14.     public int stencilSelected = 0;
    15.     public int[] bestScores = {0, 0, 0, 0};
    16.     public int[] bestConsecutives = {0, 0};
    17.     public int[] totalTouches = {0, 0, 0, 0};
    18.     public int[] totalGamesPlayed = {0, 0, 0, 0};
    19.     public DateTime rateUs = System.DateTime.Now.AddDays(1);
    20.     public TimeSpan totalPlaytime = TimeSpan.FromSeconds(0);
    21.     public bool soundOn = true;
    22.     public int adsSkipped = 0;
    23.     public bool tutorial = true;
    24.  
    25.     //Variaveis temporarias.
    26.     private float totalPlaytimeOffset = 0;
    27.     public int gameModeSelected = 0;
    28.  
    29.     void Awake ()
    30.     {
    31.         if (playerData == null)
    32.         {
    33.             DontDestroyOnLoad(gameObject);
    34.             playerData = this;
    35.         }
    36.         else if (playerData != this)
    37.         {
    38.             Destroy(gameObject);
    39.         }
    40.  
    41.         LoadData ();
    42.     }
    43.  
    44.     //Salva o PlayerData em disco.
    45.     public void SaveData ()
    46.     {
    47.         BinaryFormatter bf = new BinaryFormatter();
    48.         FileStream file = File.Create(Application.persistentDataPath + "/playerData.dat");
    49.  
    50.         PlayerData data = new PlayerData();
    51.         data.gold = gold;
    52.         data.totalGold = totalGold;
    53.         data.stencils= stencils;
    54.         data.stencilSelected = stencilSelected;
    55.         data.bestScores = bestScores;
    56.         data.bestConsecutives = bestConsecutives;
    57.         data.totalTouches = totalTouches;
    58.         data.totalGamesPlayed = totalGamesPlayed;
    59.         data.rateUs = rateUs;
    60.         data.soundOn = soundOn;
    61.         data.adsSkipped = adsSkipped;
    62.         data.tutorial = tutorial;
    63.  
    64.         UpdateTotalPlaytime ();
    65.         data.totalPlaytime = totalPlaytime;
    66.  
    67.         bf.Serialize(file, data);
    68.         file.Close();
    69.     }
    70.  
    71.     //Carrega o PlayerData do disco.
    72.     public void LoadData ()
    73.     {
    74.         if (File.Exists(Application.persistentDataPath + "/playerData.dat"))
    75.         {
    76.             BinaryFormatter bf = new BinaryFormatter();
    77.             FileStream file = File.Open(Application.persistentDataPath + "/playerData.dat", FileMode.Open);
    78.  
    79.             PlayerData data = (PlayerData)bf.Deserialize(file);
    80.  
    81.             gold = data.gold;
    82.             totalGold = data.totalGold;
    83.             stencils = CheckArrayData (stencils, data.stencils);
    84.             stencilSelected = data.stencilSelected;
    85.             bestScores = CheckArrayData (bestScores, data.bestScores);
    86.             bestConsecutives = CheckArrayData (bestConsecutives, data.bestConsecutives);
    87.             totalTouches = CheckArrayData (totalTouches, data.totalTouches);
    88.             totalGamesPlayed = CheckArrayData (totalGamesPlayed, data.totalGamesPlayed);
    89.             rateUs = data.rateUs;
    90.             totalPlaytime = data.totalPlaytime;
    91.             soundOn = data.soundOn;
    92.             adsSkipped = data.adsSkipped;
    93.             tutorial = data.tutorial;
    94.  
    95.             file.Close();
    96.         }
    97.     }
    98.  
    99.     //Faz o calculo do tempo total jogado.
    100.     public void UpdateTotalPlaytime ()
    101.     {
    102.         if (Time.realtimeSinceStartup < totalPlaytimeOffset)
    103.         {
    104.             totalPlaytimeOffset = 0f;
    105.         }
    106.  
    107.         totalPlaytime = totalPlaytime.Add(TimeSpan.FromSeconds(Time.realtimeSinceStartup - totalPlaytimeOffset));
    108.  
    109.         //Debug.Log( "Sinse Startup: " + Time.realtimeSinceStartup + " | Total Playtime Offset: " + totalPlaytimeOffset + " | Total Playtime: " + totalPlaytime);
    110.  
    111.         totalPlaytimeOffset = Time.realtimeSinceStartup;
    112.     }
    113.  
    114.     //Verifica se o array carregado eh menor do que o array da classe e faz a devida conversao. Esse erro pode ocorre quando o tamano do array eh aumentado devido a updates.
    115.     private int[] CheckArrayData (int[] arrayA, int[] arrayB)
    116.     {
    117.         if (arrayA.Length == arrayB.Length)
    118.         {
    119.             return arrayB;
    120.         }
    121.         else
    122.         {
    123.             for (int i = 0; i < arrayB.Length; i++)
    124.             {
    125.                 arrayA[i] = arrayB[i];
    126.             }
    127.             return arrayA;
    128.         }
    129.     }
    130.  
    131.     //Retorna um array de bytes contendo o PlayerData.
    132.     public byte[] GetPlayerDataToCloud ()
    133.     {
    134.         BinaryFormatter bf = new BinaryFormatter();
    135.  
    136.         PlayerData data = new PlayerData();
    137.         data.gold = gold;
    138.         data.totalGold = totalGold;
    139.         data.stencils= stencils;
    140.         data.stencilSelected = stencilSelected;
    141.         data.bestScores = bestScores;
    142.         data.bestConsecutives = bestConsecutives;
    143.         data.totalTouches = totalTouches;
    144.         data.totalGamesPlayed = totalGamesPlayed;
    145.         data.rateUs = rateUs;
    146.         data.soundOn = soundOn;
    147.         data.adsSkipped = adsSkipped;
    148.         data.tutorial = tutorial;
    149.        
    150.         UpdateTotalPlaytime ();
    151.         data.totalPlaytime = totalPlaytime;
    152.  
    153.         MemoryStream memoryStream = new MemoryStream ();
    154.         bf.Serialize (memoryStream, data);
    155.  
    156.         return memoryStream.ToArray();
    157.     }
    158.  
    159.     //Converte o array de bytes recebi e faz a leitura dos dados do PlayerData contido nele.
    160.     public void SetPlayerDataFromCloud (byte[] savedGameData)
    161.     {
    162.         BinaryFormatter bf = new BinaryFormatter();
    163.         MemoryStream memoryStream = new MemoryStream(savedGameData);
    164.  
    165.         PlayerData data = (PlayerData)bf.Deserialize(memoryStream);
    166.         gold = data.gold;
    167.         totalGold = data.totalGold;
    168.         stencils = CheckArrayData (stencils, data.stencils);
    169.         stencilSelected = data.stencilSelected;
    170.         bestScores = CheckArrayData (bestScores, data.bestScores);
    171.         bestConsecutives = CheckArrayData (bestConsecutives, data.bestConsecutives);
    172.         totalTouches = CheckArrayData (totalTouches, data.totalTouches);
    173.         totalGamesPlayed = CheckArrayData (totalGamesPlayed, data.totalGamesPlayed);
    174.         rateUs = data.rateUs;
    175.         totalPlaytime = data.totalPlaytime;
    176.         soundOn = data.soundOn;
    177.         adsSkipped = data.adsSkipped;
    178.         tutorial = data.tutorial;
    179.     }
    180.  
    181.     //Ao fechar o aplicativo salva os dados em disco.
    182.     void OnApplicationQuit()
    183.     {
    184.         SaveData();
    185.     }
    186. }
    187.  
    188. [Serializable]
    189. class PlayerData
    190. {
    191.     public int gold = 0;
    192.     public int totalGold = 0;
    193.     public int[] stencils = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    194.     public int stencilSelected = 0;
    195.     public int[] bestScores = {0, 0, 0, 0};
    196.     public int[] bestConsecutives = {0, 0};
    197.     public int[] totalTouches = {0, 0, 0, 0};
    198.     public int[] totalGamesPlayed = {0, 0, 0, 0};
    199.     public DateTime rateUs = System.DateTime.Now.AddDays(1);
    200.     public TimeSpan totalPlaytime = TimeSpan.FromSeconds(0);
    201.     public bool soundOn = true;
    202.     public int adsSkipped = 0;
    203.     public bool tutorial = true;
    204. }
    205.  

    What can i do to make this class work like Android system does?

    Thanks.
     
  2. KeiseEntertainment

    KeiseEntertainment

    Joined:
    May 5, 2015
    Posts:
    23
    No one can help me? ;/
     
  3. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    You can't realiably use BinarySerializer on iOS. You'll run into a lot of issues with iOS when trying out different serialization techniques. Welcome to a world of fun! ;)

    I recommend you look into Protobuf. It is a little more hassle to set up but the results are small file sizes and fast serialization/deserialization. Works on iOS to. Great write up here.
     
  4. KeiseEntertainment

    KeiseEntertainment

    Joined:
    May 5, 2015
    Posts:
    23
    Thanks for the answer. I'll try this out. =]