Saving Data to Single File

Sometimes, we need to save a data only to single file instead of saving it to multiple files, as it makes the management of those files harder at scale, so in this case we need to use data wrappers and move our individual data to one single data wrapper and save all of them together.

There are many approaches to workaround this issue, and here we will point to the best approaches.

Data Wrapper

Here is a simple example of Player data, such as position, score and health, at first we are saving all of this information individually to separated files:

public class Player : MonoBehaviour {

    public int score = 0;
    public float health = 100f;

    public void Save () {
        SaveGame.Save("player-position", transform.position);
        SaveGame.Save("player-score", score);
        SaveGame.Save("player-health", health);
    }

    public void Load () {
        transform.position = SaveGame.Load<Vector3>("player-position");
        this.score = SaveGame.Load<int>("player-score");
        this.health = SaveGame.Load<float>("player-health");
    }

}

Now, the below is using Data Wrapper approach to save the Player data to single file:

public class Player : MonoBehaviour {

    public PlayerData data;

    public void Save () {
        SaveGame.Save("player.dat", this.data);
    }

    public void Load () {
        PlayerData data = SaveGame.Load<PlayerData>("player.dat");
        transform.position = data.position;
    }

}

public class PlayerData {

    public Vector3 position;
    public int score;
    public float health;

}

ISavable Interface

ISavable is an interface provided by Save Game Pro to customize the serialization of a object by implementing the Save and Load methods, now here we will use the data wrapper example, but we will use ISavable interface instead of data wrapper approach:

public class Player : MonoBehaviour, ISavable {

    public int score = 0;
    public float health = 100f;

    public void Save () {
        SaveGame.Save("player.dat", this);
    }

    public void Load () {
        SaveGame.LoadInto<Player>("player.dat", this);
    }

    public void OnWrite(ISaveGameWriter writer) {
        writer.WriteProperty("position", transform.position);
        writer.WriteProperty("score", this.score);
        writer.WriteProperty("health", this.health);
    }

    public void OnRead(ISaveGameReader reader) {
        transform.position = reader.ReadProperty<Vector3>();
        this.score = reader.ReadProperty<int>();
        this.health = reader.ReadProperty<float>();
    }

}

You can also use C# ISerializable interface, but this interface is not available in UWP builds.

Custom Type

You can make a custom type for the class you want to save it’s data, and handle the serialization of the data via that custom type and save the whole data in single file. (example is in development)
Check out our Creating Custom Type article for further information.

Bonus

We can save all of the player information in a sub-directory, this approach is similar to first state, but it just makes everything ordered and easier for further manipulations:

public class Player : MonoBehaviour {

    public int score = 0;
    public float health = 100f;

    public void Save () {
        SaveGame.Save("player/position", transform.position);
        SaveGame.Save("player/score", score);
        SaveGame.Save("player/health", health);
    }

    public void Load () {
        transform.position = SaveGame.Load<Vector3>("player/position");
        this.score = SaveGame.Load<int>("player/score");
        this.health = SaveGame.Load<float>("player/health");
    }

}