Last week I made a post in Unity Answers and unfortunately haven’t had much luck with it. My problem is that I can’t save/load custom data classes to Windows Phone 8. I can successfully save and load a custom class on Android/PC devices and I can also load XML files onto Windows Phone 8. However when I try to build this version for Windows Phone 8 I get an error about Binary Fortmatter, which after some research is not available on Windows Phone 8.
Below is a snippet of code I have used to load and save the type of data I require in my game. Is there a similar method available to Windows Phone 8 which will allow me to do this? After many hours of Googling, I’ve noticed some people use PlayerPrefs, but it’s a custom class in particular I require.
public void Load_Player()
{
if(File.Exists(Application.persistentDataPath + k_PlayerInfoFileName))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + k_PlayerInfoFileName, FileMode.Open);
Player_Info data = (Player_Info)binaryFormatter.Deserialize(file);
file.Close();
m_CurrentLevel_Normal = data.m_CurrentLevel;
m_CurrentLevel = m_CurrentLevel_Normal;
m_UnlockedLevel = data.m_UnlockedLevel;
m_LevelScores = data.m_LevelScores;
m_LevelScore_Survival = data.m_LevelScore_Survival;
}
}
public void Save_Player()
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
FileStream file = null;
// Check whether a player file already exists
if(File.Exists(Application.persistentDataPath + k_PlayerInfoFileName))
{
file = File.Open(Application.persistentDataPath + k_PlayerInfoFileName, FileMode.Open);
}
else
{
file = File.Create(Application.persistentDataPath + k_PlayerInfoFileName);
}
Player_Info data = new Player_Info();
data.m_CurrentLevel = m_CurrentLevel;
data.m_UnlockedLevel = m_UnlockedLevel;
data.m_LevelScores = m_LevelScores;
binaryFormatter.Serialize(file, data);
file.Close();
}
Thank you!