Suppose you need to save, for example, the "player’s number of wins" (int WinsCount).
Add a field to PlayerProgress
This is the main data structure that gets serialized.
publicclassPlayerProgress{ // ...publicint WinsCount =0; // new field // ...}
For dictionaries: use DictionarySerializeContainer<TKey, TValue>
If you need to save a Dictionary, Unity doesn’t serialize it directly, so a special container is used.
For example, see how the WalletsData type is implemented in the project.
In DefaultPlayerProgress.cs, add a default field
This file sets the default values for new progress if no save exists.
publicclassDefaultPlayerProgress{ // ...publicint WinsCount; // default value // ...
Open the DefaultPlayerProgress config and set the desired default value in the inspector,
for example, WinsCount = 3.
In LoadProgressState.NewProgress(), you need to initialize PlayerProgress.
Create a class for reading/writing this field.
Create a MonoBehaviour or a regular class that implements both interfaces:
7. To make Zenject aware of the PlayerWinsTracker and ensure it’s properly injected and registered in the save system, add a binding method in your GameplayInstaller:
using UnityEngine;
using Zenject;
public class PlayerWinsTracker : MonoBehaviour, IProgressSaver, IProgressReader
{
private int wins;
[Inject]
public PlayerWinsTracker(ISaveLoadService saveLoadService)
{
// Making our class visible to the save system
saveLoadService.Register(this);
}
// Called by the save system to persist data.
public void UpdateProgress(PlayerProgress progress)
{
progress.WinsCount = wins;
}
// Called by the save system to load persisted data.
public void LoadProgress(PlayerProgress progress)
{
wins = progress.WinsCount;
}
// This method can be called from any appropriate location.
public void AddWin()
{
wins++;
}
}