Modifiable Serialized Data File (Unity Tips)

I wanted to take a few minutes today to share a solution to a problem that I have been facing for a while now. That problem is that once a serialized data file had been created, I couldn’t add any more variables to that data file. As of last night however, that is no longer the case. Below you will find the script that I use for serialized data, that can have more variables added on later on without requiring the user to delete and reinstall their existing data files.

[System.Serializable]
public class PersistentData : MonoBehaviour
{
public static PersistentData instance;

public ScoreData data;

public VariableType variable;
\\This is where you create your variables that will be pulled by the game. Your Serialized data will write values to these variables, whether it's a bool, int, float, etc.

private BinaryFormatter bf;
private FileStream file;
void Awake()
{
if (instance == null) {
DontDestroyOnLoad(gameObject);
instance = this;
} else if (instance != this) {
Destroy(gameObject);
}
LoadGameData();
}

public static string pathForDocumentsFile(string filename)
{
if (Application.platform == RuntimePlatform.Android) {
string path = Application.persistentDataPath;
path = path.Substring(0, path.LastIndexOf('/'));
return Path.Combine(path, filename);
} else {
string path = Application.dataPath;
path = path.Substring(0, path.LastIndexOf('/'));
return Path.Combine(path, filename);
}
}

public void SaveGameData(int i) //This is only called to save data by your game. The LoadData function is what contains the script elements to create the serialized data file.
{
string path = pathForDocumentsFile("ArcherDash2GameData.dat");
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Write);
data.variable = variable;
bf.Serialize(file, data);
file.Close ();
}

public void LoadGameData()
{
bool fileExists = false;
string path = pathForDocumentsFile ("ArcherDash2GameData.dat");
Debug.Log (path);

if (!File.Exists (path)) {
data = new ScoreData ();
bf = new BinaryFormatter ();
file = new FileStream (path, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
fileExists = false;
} else {
bf = new BinaryFormatter ();
file = new FileStream (path, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
data = (ScoreData)bf.Deserialize (file);
fileExists = true;
}

//The following chunk of code is how you create and assign value to each of your variables, you will have this chunk repeated for each variable. It's this chunk of code here that enables you to be able to add variables later on. This is what sets my code apart from other serialized data file creators that you can find on the web. With those other code bases, if you need to add a variable the existing saved data file has to be deleted, otherwise the variable you want to add will be non-existent, resulting in a null variable.

if (data.variable != null && fileExists) {
variable = data.variable;
} else {
data.variable = (Desired definition for a new file);
variable = data.variable;
}
bf.Serialize (file, data);
file.Close ();
}
}

[System.Serializable]
public class ScoreData
{
//Class of your variables. This is what is used to create the variables for the serialized data file.
}

Feel free to use this in your own projects, and if you have any questions, or suggestions to make the code better, please feel free to share!