Save File - Unity-Technologies/EndlessRunnerSampleGame GitHub Wiki

The game use a local save file to save highscore, owned characters, accessories & themes and current currencies.

This is to keep the project simple and working locally without having to setup a server back-end, but in a "commercial" products, we would probably want to save that on a remote server for validation with an account system link to phone or mail. With a local version only, someone could open their save file and grant themselves lots of premium currency easily.

Versioning

The save file use a versioning system that allow to not break the save when modification is made to the game.

It work by defining a const integer value that is the current version of the save file (called s_Version in PlayerData in this case). We modify this value every time we add a new thing to the save or modify how we save something in it.

Every time a save file is written, it write the version as the first value in the save file.

Then when a save file is read, we can use that version to know if the save file contain certain value, and either read them (if version is recent enough) or initialize them to default value if it is not.

E.g. We want to add saving the latest used accessory to the save file. We bump the version of our save file from 1 to 2, and we modify the write function to write the name of the accessory in the save file. We then modify the read function to check for the save file version before reading the accessory name. if the version is >= 2, then we read the name from the save file, if it is < 2, then the save file is from a previous version of the game that don't contain that data, so we initialise current accessory to none.