Read write data for Windows Store/Phone C#

Unity for Windows Store/Phone don’t support System.IO and BinaryFormatters. How do I do the following without them:

I’m trying to serialize my GameProgress (level scores) which is basically:

GameProgress gameProgress = List<LevelProgress>();
LevelProgress:
 ---- levelName (string)
 ---- time (float)
 ---- score (int)

For other platforms, I do this:

BinaryFormatter bf = new BinaryFormatter();
FileStream fs = File.Create(filename);
bf.Serialize(fs, gameProgress);
fs.Close();

I’m having trouble with compiling a plugin for Windows that handles this for me as the namespaces clash with other plugins I’m using.

I was hoping to use UnityEngine.Windows.File.WriteAllBytes() and ReadAllBytes() instead, but could not figure out how to convert my data to an array of Bytes.

Does someone have a code example they can show me?

I’m going to answer my own question. It took me a month, but I finally have a solution that works.

The solution is to create a Windows Phone Plugin, but you also need to create a dummy plugin for Unity to use.
Here’s a guide on how to do this:
http://docs.unity3d.com/Manual/wp8-plugins-guide-csharp.html

However, I found it difficult to create the plugin because I did not have a professional license of Visual Studio. I ended up using Visual Studio Express 2012 to create the Unity plugin and Visual Studio 2012 for Windows Phone to create the WP plugin. I then copied the WP project into the VS 2012 solution and was able to work on both together.

Credit for the serialization code goes to Carlos Figueira on this thread .

PLUGIN CODE

In my Real DLL project, I have a class called FileIO, with 2 functions:

using System.Runtime.Serialization;

namespace UnityWindowsPhonePlugin
{

    public class FileIO
    {
        
        public static byte[] SerializeObject<T>(T obj)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(ms))
                {
                    DataContractSerializer dcs = new DataContractSerializer(typeof(T));

                    dcs.WriteObject(writer, obj);
                    writer.Flush();
                    return ms.ToArray();
                }
            }
        }

        public static T DeserializeObject<T>(byte[] obj )
        {
            using (MemoryStream memoryStream = new MemoryStream(obj))
            {
                using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(memoryStream, XmlDictionaryReaderQuotas.Max))
                {
                    DataContractSerializer dcs = new DataContractSerializer(typeof(T));
                    return (T)dcs.ReadObject(reader);
                }
            }

        }

    }
}

For my Fake DLL, I implement fake versions of the same classes.

namespace UnityWindowsPhonePlugin
{
    public class FileIO
    {

        public static byte[] SerializeObject<T>(T obj) 
        {
            return new byte[0];
        }

        public static T DeserializeObject<T>(byte[] obj) 
        {
            return default(T);
        }
    }
}

UNITY CODE

Copy your Real DLL to Assets\Plugins\WP8, and your fake DLL to Assets\Plugins.

In Unity, this is how I save my game progress:

#if UNITY_WINRT
		byte[] bytes = UnityWindowsPhonePlugin.FileIO.SerializeObject<GameProgress>(this);
		File.WriteAllBytes(getSaveFile(), bytes);
#else
		BinaryFormatter bf = new BinaryFormatter();
		FileStream fs = File.Create(getSaveFile());
		bf.Serialize(fs, this);
		fs.Close();
#endif

And I load my Game Progress like this:

byte[] bytes = File.ReadAllBytes(saveFile);		
GameProgress gp = UnityWindowsPhonePlugin.FileIO.DeserializeObject<GameProgress>(bytes);

Hope that helps others who are stuck with the same problem I was. Let me know if you need help with the plugin. I might be able to send you my VS project.