- Home /
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 could do that, but it gets pretty cumbersome for a complex data type. I would have to save dictionary keys such as "World1Level1Time", "World1Level2Score" etc.
Also, there's the 1$$anonymous$$B limit. I don't think I'd hit that right now, but that could limit my save data in the future.
Answer by psykojello2 · Jun 22, 2014 at 11:00 PM
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.
Could you attach the dll and the dummy? thanks! (I'll try to create the plugin myself tho)
Is there any way one could use Sharp Serializer for this purpose? I am currently attempting your solution for my project. Apparently the plug in is able to replace the purpose of the built in binary formatted, although I'm not sure how... (I'm new to serializes and am starting work on a project that has been in development). I'm going to keep stabbing away at it, but if you happen to notice this comment, any sort of help would be much obliged :)
Thanks Ben
Hi Ben, just saw your comment. Unfortunately I'm not too familiar with Sharp serializer or any of the plugin stuff really :) In fact I abandoned Windows Phone since my last post. It just became too much work trying to get plugins to behave. I'll just build for W10 at some point.