- Home /
Android: Trying to reset save data on device when pushing a new build!
Hi, all!
So I'm working on an Android project with some basic progression information being saved between game instances. To accomplish this, I'm reading and writing to a save file at Application.persistentDataPath
, and serializing / deserializing a custom savedata class.
All the save/load functionality works fine on my test devices -- data persists as expected when I quit and reopen the app. The problem is, when I push new builds to a device that's already run the program before, the save data from that older build is not being reset, causing some big problems with my ability to test. Even though it's a fresh build, all my progression information from a previous build is surviving!
So my question is -- how can I reset the file saved at Application.persistentDataPath
on an Android device whenever I push a new build? I'll say that in a perfect world, I'd like to generally be able to access the Application.persistentDataPath
from the Unity editor (on Windows), so as to be able to generate and push "Dummy" saves -- but the base-level goal is just being able to reset the device's save data with each build.
I'm not sure if this is necessary for my question, but just in case, here's the save / load code I'm using:
public static void Save(SAVEDATA_CLASS saveData)
{
if (saveData != null)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + saveFileName, FileMode.OpenOrCreate);
bf.Serialize(file, saveData);
file.Close();
}
}
public static SAVEDATA_CLASS Load()
{
SAVEDATA_CLASS output;
if (File.Exists(Application.persistentDataPath + saveFileName)) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + saveFileName, FileMode.Open);
output = (SAVEDATA_CLASS)bf.Deserialize(file);
file.Close();
}
else {
output = new SAVEDATA_CLASS();
}
return output;
}
I'm not an expert, but I think the savegame needs some kind of knowledge about the version it was saved by. The same goes for the version for the game. You could save a version tag to the savefile, and whenever you read it, compare it to the version of your game (build version maybe, or just a static variable in some code).
This way you could just delete it when the version differs and write a new file.
Answer by elenzil · Oct 21, 2016 at 09:10 PM
try something like this.
private const int version = "2";
bool thisIsADifferentVersion() {
int previousVersion = PlayerPrefs.getInt("appVersion", version);
return previousVersion != version;
}
void checkAndHandleNewVersion() {
if (thisIsADifferentVersion()) {
// reset stuff
PlayerPrefs.setInt("appVersion", version);
}
}
.. then with each release, bump the value of "version".
Your answer
Follow this Question
Related Questions
internal persistentDataPath with WRITE_EXTERNAL_STORAGE permission 4 Answers
Reading files on Android, 1 Answer
Can other android apps delete save data of Application.persistentDataPath? 1 Answer
PersistentDataPath saved files are not writing into android device? 1 Answer
Using the Android default debug keystone from Windows 1 Answer