8000 GitHub - oguzhalil/savefile: Saving files using C# binary formatter in Unity
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

oguzhalil/savefile

Repository files navigation

SaveFile Quick Start

@NOTE: for custom classes make sure you add System.Serializable attribute

[System.Serializable]
public class Foo 
{
   public int bar;
}

Save File Usage

Initialize

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);
 }

Set

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);

Get

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());

Save File

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();
    }
 }

About

Saving files using C# binary formatter in Unity

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0