@NOTE: for custom classes make sure you add System.Serializable attribute
[System.Serializable]
public class Foo
{
public int bar;
}
Initialize local savefile object with given unique identifier.
Either at playerprefs or persistentpath location.
Register a method for error handling.
savefile = SaveFile.PlayerPrefs(fileId , OnFileError , false);
// or savefile = SaveFile.PersistentPath(fileId , OnFileError , false);
private void OnFileError(FileError error)
{
Debug.LogError(error);
}
You can save any object supported by binary formatter.
int,byte,float,string,classes,struct etc.
Set<T>(string key, T value)
creates or updates given key-value pair
savefile.Set("someInteger", 5);
GameConfig writeConfig = new GameConfig();
savefile.Set("gameConfig", writeConfig);
For fail safe Get<T>(string key, T defaultValue)
method requires defaultValue
If given key-value pair is not exists then defaultValue will be returned
int integer = savefile.Get("someInteger" , 0);
GameConfig readConfig = savefile.Get("gameConfig", new GameConfig());
File is not saved to disk until you call savefile.Sync()
method
savefile.Sync() blocks UIThread until operation is completed.
NOTE: Dont save huge files(10 mb or more) when processing an exit message OS may suspend your operation and
you may end up with broken save file.
// can be called any time during the game.
savefile.Sync();
// on application killed - make sure file is saved.
private void OnApplicationQuit()
{
savefile.Sync();
}
// on application suspended (home button) - make sure file is saved.
private void OnApplicationPause(bool pause)
{
if(pause)
{
savefile.Sync();
}
}