Hello everyone.
I have made this game for Android, and it saves its data as file using the Application.persistentDataPath + " /" + <fileNameHere> path.
For most Android devices, this goes on smoothly and there are no issues. However, rarely, the files can get completely deleted from the user’s device. I’ve looked into it, but I couldn’t find any answers as to why this would happen. I got to know of this from angry user reviews.
I know it has to to with the files being deleted (as opposed to corrupted) because it recently occurred on my device too. And searched for the files, and they were completely gone. There’s nothing in my game that can do this. I’m suspecting that it may have to do with the Android booting process (like power on and restarts) but I’m not certain.
This is a sample of the code from my saving system so that you can get an idea of how it works. There’s nothing special about it actually.
const string fileName = "/savedData.dat";
public void saveHiScore(int hiScore)
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + fileName);
//Formatting the hiScore into the serializable PlayerData class
PlayerData data = new PlayerData(hiScore, globalDataPreserver.Instance.coinCount);
//Storing the data
binaryFormatter.Serialize(file, data);
file.Close();
}
public int loadHiScore()
{
if (File.Exists (Application.persistentDataPath + fileName))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + fileName, FileMode.Open);
//Retrieving the saved data in the fomr of a PlayerData class
PlayerData data = (PlayerData)binaryFormatter.Deserialize(file);
file.Close();
return data.hiScore;
}
else
{
return 0;
}
}
Does anyone have any experience with something like this?
Do you know of any safe path location I could save backup files separate form the Application.persistentDataPath?