I have a lengthier Blog Post about Unity File I/O and the danger of PlayerPrefs here, but I'll give a quick summary in this post. Of course, I'll be focusing on how I did it for Frog Snatchers.
The two main things to note are that PlayerPrefs is not a good way to save pertinent game-data, and that I've chosen BinaryFormatters to serialize our data instead. PlayerPrefs saves data in an easily accessible (and easily editable) format in your registry. This prevents us from taking that save data and moving it onto a different computer, as well as making all of the data we put there completely vulnerable. In the post above, I briefly talk about how this made it possible for me to give myself any item I wanted in a game that saved data with PlayerPrefs.
Instead, we're using BinaryFormatters, which are really easy to use in Unity C#. The basic step by step is as follows:
- Create a new, public class that will store all the data we want saved
- In this case, that includes SceneID, PlayerPosition, PlayerStats, ControllerPreferences, and Flags
- In a Save() function, grab all of the data from the appropriate places in the game and populate a new instance of this class
- Open (or create a new) Save File using File.Open(...)
- Create a new BinaryFormatter, then call BinaryFormatter.Serialize(File, Data);
- For Loading(), it's basically steps 2-4 again, but data moves the other way
- We verify the save file exists, then Open it
- We create a new BinaryFormatter, and call Deserialize on the File, storing its data in a new instance of that public class (this requires a cast)
- Then we take the data from the public class and distribute it throughout the game
I actually have a separate function for just saving Controller Data, but it follows the same process, so I'm going to use images of that. It's far more condensed and easy to read than the functions and class used for the rest of the entire game's data!