Created
April 27, 2021 15:33
-
-
Save antpaw/794e5c964c2f4932dea06a2ef9bd0947 to your computer and use it in GitHub Desktop.
SaveData Serialization to Unity Filesystem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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