Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Serialization not working with Int

Discussion in 'Scripting' started by mancinism, Apr 7, 2015.

  1. mancinism

    mancinism

    Joined:
    Apr 7, 2015
    Posts:
    2
    I have tried multiple example to save/load game data using serialization, yet I still facing problem with Integer.

    I created a class for the game data:
    Code (CSharp):
    1.  
    2. [Serializable]
    3. class PlayerData
    4. {
    5.     public string sz;
    6.     public int inum;
    7.     public float fnum;
    8. }
    Data saving function:
    Code (CSharp):
    1. using (FileStream fs = File.OpenWrite(Application.persistentDataPath + "/angry.wn"))
    2. {
    3.     BinaryFormatter bf = new BinaryFormatter();
    4.    
    5.     PlayerData data = new PlayerData();
    6.    
    7.     data.sz = "12345";
    8.     data.inum = 12345;
    9.     data.fnum = 12345.0f;
    10.     bf.Serialize(fs, data);
    11. }
    Data loading function:
    Code (CSharp):
    1. if (File.Exists(Application.persistentDataPath + "/angry.wn"))
    2. {
    3.     using(FileStream fs = File.OpenRead(Application.persistentDataPath + "/angry.wn"))
    4.     {
    5.         BinaryFormatter bf = new BinaryFormatter();
    6.      
    7.         PlayerData data = (PlayerData)bf.Deserialize(fs);
    8.  
    9.         Debug.Log(" >>> Read string:" + data.sz + " int:" + data.inum + " float:" + data.fnum);
    10.  
    11.     }
    12.  
    13. }
    And this is what I get from Debug Log

    >>> Read string:12345 int:57 float:12345

    It just wont work with Integer and Long. Really appreciate if anyone could help me sort out the problem. Thanks.

    It was developed using Unity5.0 for iOS Devices.
     
    Last edited: Apr 7, 2015
  2. debosotnas

    debosotnas

    Joined:
    Mar 6, 2013
    Posts:
    4
    Weird case, but (in meantime)... why just not use other type that is working in your case and then parse the value ?
     
  3. Galf

    Galf

    Joined:
    Feb 24, 2013
    Posts:
    27
    So, this isn't a solution, but something you might want to look into:
    0011 0000 0011 1001 = 12345
    0000 0000 0011 1001 = 57

    So it seems it is either serializing ints as a single byte, or for some reason not reading the high-order byte back. No idea why -- if you don't figure it out, I'll look into it later tonight.
     
    Mycroft likes this.
  4. Galf

    Galf

    Joined:
    Feb 24, 2013
    Posts:
    27
    Well, I tested on Unity 4.6 building for iOS in the Editor, and everything serialized fine. Is this giving the same results for you in the editor? Or only on the device itself? If you see the behavior in the editor as well, is there any possibility you are manipulating the int prior to outputting it? i.e. accidentally bitwise-AND-ing it, doing some really strange bit-shifting, casting, etc?
     
  5. mancinism

    mancinism

    Joined:
    Apr 7, 2015
    Posts:
    2
    It is working fine with the Editor.

    The error just happened on the iOS devices including the iOS Simulator. I think iOS is somehow think the INT is only a byte length. The value are kept with 0-255 as you mentioned previously.
     
  6. meril

    meril

    Joined:
    Oct 22, 2014
    Posts:
    1
    Have exactly the same issue.

    The code below works fine under OSX.

    Under iOS with scripting backend IL2CPP, the INT and DATETIME became a wrong value (value % 256, i.e. if value = 1000, the new value will be 232).
    Under iOS with scripting backend Mono (2.x), everything is OK.

    Here the test code. The function "Check" is based on the solution found here:
    http://answers.unity3d.com/questions/753008/problem-when-saving-a-list-using-player-prefs.html

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using System.Runtime.Serialization.Formatters.Binary;
    7. using System.IO;
    8.  
    9. [System.Serializable]
    10. public class Item {
    11.    public int score;
    12.    public DateTime date;
    13. }
    14.  
    15. public class Test : MonoBehaviour {
    16.  
    17.    // Use this for initialization
    18.    void Start () {
    19.  
    20.      List<Item> before = new List<Item>();
    21.      List<Item> after = new List<Item>();
    22.  
    23.      // Create the test value
    24.      Item example = new Item ();
    25.      example.score = 1000;
    26.      example.date = DateTime.Now;
    27.  
    28.      // Insert value into list
    29.      before.Add (example);
    30.  
    31.      // Check serialize / deserialize
    32.      after = Check<List<Item>>(before);
    33.  
    34.      // Print
    35.      Print ("before", before);  
    36.      Print ("after", after);
    37.  
    38.    }
    39.  
    40.    void Print (string label, List<Item> items) {
    41.      foreach (Item item in items) {
    42.        Debug.Log (label + ": " + item.score.ToString () + " / " + item.date.ToString ());
    43.      }    
    44.    }
    45.    
    46.    T Check<T> (T _saveMe) {
    47.      BinaryFormatter _bin = new BinaryFormatter ();
    48.      MemoryStream _mem = new MemoryStream ();
    49.      _bin.Serialize (_mem, _saveMe);
    50.    
    51.      BinaryFormatter _bin2 = new BinaryFormatter ();
    52.      MemoryStream _mem2 = new MemoryStream (_mem.GetBuffer ());
    53.      T _obj = (T) _bin2.Deserialize (_mem2);
    54.      return _obj;
    55.    }
    56. }
    57.  
    58.  

    === OUTPUT ===

    iOS with IL2CPP:

    Code (csharp):
    1.  
    2. before: 1000 / 04/09/2015 14:34:42
    3. after: 232 / 01/02/0001 01:46:11
    4.  
    iOS with Mono (2.x) or OSX:
    Code (csharp):
    1.  
    2. before: 1000 / 04/09/2015 14:34:42
    3. after: 1000 / 04/09/2015 14:34:42
    4.  
     
    Last edited: Apr 9, 2015
  7. winzcrieg

    winzcrieg

    Joined:
    Apr 13, 2015
    Posts:
    7
    Hi,

    We are having the same issue with integer and date values on iOS devices (iOS 8.3) with Unity 5.0.0f4 (Backend : IL2CPP). It works fine in the Editor on OSX 10.10.3.
    We checked the issue tracker but couldn't find a bug report for this.

    Have any of you raised a bug or issue for this? If not we will raise a bug.

    Thank you.
     
  8. winzcrieg

    winzcrieg

    Joined:
    Apr 13, 2015
    Posts:
    7
    Hi,

    We have submitted the Bug (Case 694713) with Unity for this.

    Thank you.
     
  9. winzcrieg

    winzcrieg

    Joined:
    Apr 13, 2015
    Posts:
    7
    hi,

    It looks like the issue is not occurring in Unity 5.0.1f1.