I tried similar code in Javascript and there are no warnings in the Editor, but hangs the device. Looking at the Xcode console, I guess System.Runtime.Serialization is simply not supported by the Mono AOT compilation. But that’s just a guess! Did you bug report this? Just curious if you are still working on this method.
var fs : FileStream = new FileStream(getSavePath() + "/test.dat", FileMode.Create);
var formatter : BinaryFormatter = new BinaryFormatter();
formatter.Serialize(fs, test);
I am currently using XML serialization but would like to use binary serialization for some things.
The following code works in the simulator but will crash on the device with a similar error as posted above.
using UnityEngine;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
public class Deserialize : MonoBehaviour
{
// Use this for initialization
void Start()
{
Person p = new Person();
p.Name = "Ted";
byte[] objectData;
//serialize
Debug.Log("Serializing");
IFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, p); //This crashes on the device
objectData = stream.GetBuffer();
stream.Close();
//deserialize
Debug.Log("Deserializing");
formatter = new BinaryFormatter();
stream = new MemoryStream(objectData);
Person person = (Person)formatter.Deserialize(stream);
Debug.Log(person.Name);
}
}
[Serializable]
public class Person
{
public string Name;
}
Is there any word if this will be fixed in a future release or if there is a workaround for this problem?