- Home /
How do you serialize things on Windows Phone?
I've pretty much done the serialization as in the Mike Geig's live training video.
The thing is that Unity allows me to build my project on Android. But when I try to build it on Windows Phone it says that System.Runtime.Serialization doesn't have Formatters.
So how do you do serialization on Windows Phone?
Here's my code:
using UnityEngine;
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
public class SaveIt : MonoBehaviour {
public float health = 0;
public float mightyPowers = 0;
public void Save()
{
Debug.Log("Saving");
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/player.dat");
PlayerData data = new PlayerData();
data.health = health;
data.mightyPowers = mightyPowers;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
BinaryFormatter bf = new BinaryFormatter();
if (File.Exists(Application.persistentDataPath + "/player.dat"))
{
Debug.Log("Loading " + Application.persistentDataPath + "/player.dat");
FileStream file = File.Open(Application.persistentDataPath + "/player.dat", FileMode.Open);
PlayerData data = (PlayerData) bf.Deserialize(file);
file.Close();
health = data.health;
mightyPowers = data.mightyPowers;
}
}
// Use this for initialization
void Start () {
Load();
}
}
[Serializable]
class PlayerData {
public float health;
public float mightyPowers;
}
It's possible that the libraries that the windows phone uses aren't the same. Since WP uses .NET 4.x, take a look at those libraries and see where the serializer classes are and how they are used. That is the main difference between WP and iOS/Android
$$anonymous$$ike Geig, could you explain how to save on Windows Phone 8? I have the same problem, i was following the live training too.
I have the same problem. I got the same code from the live training. I can build the game, but i become an error:
Reference Rewriter: Error: method System.Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter::Deserialize(System.IO.Stream)
doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at System.Void GameControl::Load().
UnityEngine.Debug:LogError(Object) PostProcessWP8Player:RewriteReferencesForAssembly(String, String, String, String, String, String, String) (at C:/buildslave/unity/build/PlatformDependent/WP8Player/Extensions/$$anonymous$$anaged/PostProcessWP8Player.cs:450) PostProcessWP8Player:$$anonymous$$ake$$anonymous$$anagedAssembliesWp8Compatible(String, String, LibraryCollection) (at C:/buildslave/unity/build/PlatformDependent/WP8Player/Extensions/$$anonymous$$anaged/PostProcessWP8Player.cs:411) PostProcessWP8Player:PostProcess(BuildTarget, BuildOptions, String, String, String, String, String, Guid) (at C:/buildslave/unity/build/PlatformDependent/WP8Player/Extensions/$$anonymous$$anaged/PostProcessWP8Player.cs:212) UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()
Hello Vekoro! I ended up using a commercial plugin called Easy Save 2. https://www.assetstore.unity3d.com/en/#!/content/768
With the plugin you can do serialization on the Windows Phone. But I'm not completely sure if it's the fastest solution when considering performance.
Let's hope that we will have better performance with WP 10 in overall when compared to other mobile platforms.
Your answer
Follow this Question
Related Questions
Array of arrays = No serialization? 4 Answers
Unbound Objects Are Not Null? 0 Answers
Serialization errors.. 0 Answers
Is EditorUtility.SetDirty restricted to prefabs or inspected GameObject? 5 Answers