Basic Saving and Loading
First, you need to add using BayatGames.SaveGamePro;
at top of your script to use Save Game Pro API.
Here is a simple usage example of Save Game Pro by saving and loading a simple integer value:
// The score value
int score = 12;
// The default score value, used when the saved value does not exists
int defaultScore = 0;
// Saving the score
SaveGame.Save<int> ( "score", score );
// Loading back the score
score = SaveGame.Load<int> ( "score", defaultScore );
As you can see, Save Game Pro saves values using identifiers and allows you to load desired value using the specified identifier.
Now, let us save some more complex data, let us save a struct:
Vector3 position = new Vector3 ( 10, 20, 30 );
// Saving the position
SaveGame.Save ( "position", position );
// Loading the position back, if not exists, set the value to Vector3.zero
position = SaveGame.Load<Vector3> ( "position", Vector3.zero );
Also, you can save Almost All Components:
// Saving the Transform component of this Game Object
SaveGame.Save ( "transform", transform );
// Loading the transform data into this Transform component
SaveGame.LoadInto ( "transform", transform );
We have two kinds of saving and loading, saving and loading by Value and saving and loading by Reference.
Saving and Loading by Value
Most of the types are Value types, such as Primitives, Non-MonoBehaviour classes, Structs, … these data types can be saved by value:
string helloWorld = "Hello, World!";
// Saving the string
SaveGame.Save ( "helloWorld", helloWorld );
// Loading the string
helloWorld = SaveGame.Load<string> ( "helloWorld", "Goodbye, World!" );
Note: All classes, except MonoBehaviours and Unity Objects, can be saved and loaded by both Reference and Value, a good example for this type of data is Mesh class, this class can be stored as Value or Reference and loaded as Value or Reference without any problem:
Mesh mesh = new Mesh ();
// Saving the mesh
SaveGame.Save ( "mesh", mesh );
// Loading the mesh by Value
mesh = SaveGame.Load ( "mesh" );
// Loading the mesh by Reference
SaveGame.LoadInto ( "mesh", mesh );
Saving and Loading by Reference
You can save and load all classes and components by reference because the Classes are Reference Types in C# and any change to the reference reflects the source:
Transform myTransform = transform;
// Saving the transform
SaveGame.Save ( "myTransform", myTransform );
// Loading the transform data to the myTransform
SaveGame.LoadInto ( "myTransform", myTransform );
Note: It is not recommended to save and load MonoBehaviours, Components, Unity Objects, … by Value, because they will be instantiated as needed.
Encryption
Note: This feature has been introduced in version 2.8.0
Sometimes you need to save your data securely that no one else can modify it to cheat your game, so you need encryption in this case that Save Game Pro uses AES encryption, first of all you need to enable encryption in Save Game Pro but settings Encrypt property of SaveGameSettings to true, like this:
SaveGameSettings settings = new SaveGameSettings();
settings.Encrypt = true;
settings.EncryptionPassword = "this is my encryption password";
SaveGame.Save("encrypted.dat", "Sample Data", settings);
// Also make sure to use the same password for loading the data!
Or you can change the default settings and enable encryption globally for all Save calls:
SaveGameSettings settings = SaveGame.DefaultSettings;
settings.Encrypt = true;
settings.EncryptionPassword = "This is my encryption password";
SaveGame.DefaultSettings = settings;
// Then when you call SaveGame.Save or SaveGame.Load without passing settings argument, then it will use default settings as we have modified
// This will encrypt and use the encryption password provided by default settings
SaveGame.Save("encrypted.dat", "Sample Data");