Rob - Saving and Loading - Senior Production Blog Post #8

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:

  1. Create a new, public class that will store all the data we want saved
    1. In this case, that includes SceneID, PlayerPosition, PlayerStats, ControllerPreferences, and Flags
  2. In a Save() function, grab all of the data from the appropriate places in the game and populate a new instance of this class
  3. Open (or create a new) Save File using File.Open(...)
  4. Create a new BinaryFormatter, then call BinaryFormatter.Serialize(File, Data);
  5. For Loading(), it's basically steps 2-4 again, but data moves the other way
  6. We verify the save file exists, then Open it
  7. 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)
  8. 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!

 The new, Public class. Note that it needs that System.Serializable tag, or else you can't save it!

The new, Public class. Note that it needs that System.Serializable tag, or else you can't save it!

 Saving Controller Data with my two InputManager classes - Differentiated by the letters at the end (PS vs RMG)

Saving Controller Data with my two InputManager classes - Differentiated by the letters at the end (PS vs RMG)

 Loading the data, then giving both InputManagers their respective data!

Loading the data, then giving both InputManagers their respective data!