- Home /
Saving serialized data for iOS
I am trying to save system C# serialized data to iphone. The problem is that it isn't saving data. This code runs perfect on my Android device, but fails on iPhone. Here's the code:
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
private string filePath = "playerInfo.dat";
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create (Application.persistentDataPath + "/" + filePath);
PlayerData data = new PlayerData();
data.instructionsCompleted = instructionsCompleted;
data.totalStarsCollected = totalStarsCollected;
data.highscore = highscore;
data.pandaLocked = pandaLocked;
data.juggLocked = juggLocked;
data.demonLocked = demonLocked;
data.pandaTrailActive = pandaTrailActive;
data.juggTrailActive = juggTrailActive;
data.demonTrailActive = demonTrailActive;
bf.Serialize (file, data);
file.Close ();
}
public void Load()
{
if(File.Exists (Application.persistentDataPath + "/" + filePath))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open (Application.persistentDataPath + "/" + filePath, FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize (file);
file.Close();
instructionsCompleted = data.instructionsCompleted;
totalStarsCollected = data.totalStarsCollected;
highscore = data.highscore;
pandaLocked = data.pandaLocked;
juggLocked = data.juggLocked;
demonLocked = data.demonLocked;
pandaTrailActive = data.pandaTrailActive;
juggTrailActive = data.juggTrailActive;
demonTrailActive = data.demonTrailActive;
}
else
{
instructionsCompleted = true;
totalStarsCollected = 0;
highscore = 0;
pandaLocked = true;
juggLocked = true;
demonLocked = true;
pandaTrailActive = false;
juggTrailActive = false;
demonTrailActive = false;
}
Save ();
}
[Serializable]
class PlayerData
{
public bool instructionsCompleted;
public int totalStarsCollected;
public int highscore;
public bool pandaLocked;
public bool juggLocked;
public bool demonLocked;
public bool pandaTrailActive;
public bool juggTrailActive;
public bool demonTrailActive;
}
What I do notice is the first time I load app all my data is right. I am assuming it's running the else statement in Load() method. When I close app, double tap home button, and close out app, and when I reopen it all my data is reset, but I do notice that my booleans that should be true are now false. I believe this is because it is running the if statement in the Load() method, and all my data is returning null. A null boolean is a false statement. Any ideas?
Your answer
Follow this Question
Related Questions
Undo.RecordObject works with GUI.Button but not with GUILayout.Vector3Field 1 Answer
Unity Saved Serialized Levels 0 Answers
Is there a way to save a procedurally-generated mesh on the client machine? 1 Answer
Serializing and restoring full Animation state 0 Answers
Save and load serialization problem 0 Answers