Skip to content

Instantly share code, notes, and snippets.

@antpaw
Created April 27, 2021 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antpaw/794e5c964c2f4932dea06a2ef9bd0947 to your computer and use it in GitHub Desktop.
Save antpaw/794e5c964c2f4932dea06a2ef9bd0947 to your computer and use it in GitHub Desktop.
SaveData Serialization to Unity Filesystem
public class Something
{
SaveData saveData;
void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(FileLocation());
bf.Serialize(file, saveData);
file.Close();
}
public void Load()
{
if (File.Exists(FileLocation()))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(FileLocation(), FileMode.Open);
saveData = (SaveData) bf.Deserialize(file);
file.Close();
}
else
{
saveData = new SaveData();
}
}
string FileLocation()
{
return Application.persistentDataPath + "/save_data.gd";
}
}
[Serializable]
public class SaveData
{
public bool soundMuted;
public bool musicMuted;
public bool vibration;
public int score;
public string language;
public int[] maxUnlocked;
public SaveData()
{
soundMuted = false;
musicMuted = false;
vibration = true;
SetBlankProgress();
}
public void SetLanguage(string newLanguage)
{
language = newLanguage;
}
public void SetBlankProgress()
{
score = 0;
maxUnlocked = new int[]
{
0,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment