Can I add or remove values and variables in persistentDataPath once it's been created?
Can I remove or add new values and variables to persistentdatapath when I update an application? If yes, how I can do it?
Also can I search for the file that was created to save the data and edit the values in it?
Could you post your code so that we can get an idea of what you are doing?
Answer by Darkeriol · Aug 03, 2017 at 03:10 PM
public class SaveLoad : MonoBehaviour {
public void Save()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/playerInfoITH.dat");
PlayerData data = new PlayerData();
data.lang = Data.lang;
data.music = Data.music;
data.uwarriortest = Data.uwarriortest;
data.uwarrior = Data.uwarrior;
data.uarcher = Data.uarcher;
data.coin = Data.coin;
bf.Serialize (file, data);
file.Close ();
}
public void Load()
{
if (File.Exists (Application.persistentDataPath + "/playerInfoITH.dat"))
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/playerInfoITH.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize (file);
file.Close ();
Data.lang = data.lang;
Data.music = data.music;
Data.uwarriortest = data.uwarriortest;
Data.uwarrior = data.uwarrior;
Data.uarcher = data.uarcher;
Data.coin = data.coin;
}
}
public void Delete()
{
if (File.Exists (Application.persistentDataPath + "/playerInfoITH.dat"))
{
File.Delete (Application.persistentDataPath + "/playerInfoITH.dat");
}
}
}
[Serializable]
class PlayerData
{
//Settings
public int lang;
public bool music;
//Characters
public bool uwarriortest;
public bool uwarrior;
public bool uarcher;
public int coin;
}
My question is whether I can add new variables or remove them to the PlayerData class when I update my application.
Example:
class PlayerData
{
//Settings
public int lang;
public bool music;
//Characters
public bool uwarriortest; //Can I remove it?
public bool uwarrior;
public bool uarcher;
public bool uwizard; // Can I add it?
public int coin;
}
I think you can, just make sure to delete the old variable from the playerdata to avoid "clutter".
Is there a script that allows me to delete the variables I do not need? Because if I try to delete it in the PlayerData class script once the variable has been created by an earlier update, it gives me an error.
SerializationException: Field "uwarriortest" not found in class PlayerData
Is there a reason you are using .dat files? In the past, I have used playerprefs and they work just fine for me. They are also much easier to use.
Your answer
Follow this Question
Related Questions
PersistentData erased with iOs update 0 Answers
Press Escape, quit game if variable is false otherwise... How? 1 Answer
Optimizations: Should I use Update or FixedUpdate? Could you help me optimize rotations? 1 Answer
Start() and Update() execution issue 6 Answers
Stuck with scaling an object 1 Answer