BinaryFormat.Serialize locking up on ipod, but not editor

I’m trying to serialize a simple class:

	[Serializable]
	class Header
	{
		public enum Key {XML};
		public Key key;
		public int length;
	}

…to a MemoryStream, using a BinarySerializer

			Header h = new Header();
			h.key = Header.Key.XML;
			h.length = buffer.Length;
			bf.Serialize(ms, h);

if I comment out the last line, it doesn’t crash.

It sure seems broken, but I thought I’d check before filing a bug.

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 bug reported this the other day case 122072.

Cheers,
-Jon

I ended up just using XML Serialization, which works perfectly.

Thanks Amazing- I will check that out.

Are you using Standard or Advanced? With Standard I get errors when trying to build for iPhone.

import System.Xml;
import System.Xml.Serialization;
import System.IO;

I guess I am still feeling my way around what one can and cannot do with Standard.

I’m using advanced…I was getting the very same error, and decided to shell out the cash. Wasn’t very happy about it though.

If you want to get by on the cheap you can probably do something with playerprefs.

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?

Hi,
please submit bug report with sample project attached to it.