Give Page Feedback | API

Persistent State Managers - Binary Serializer Manager

The Binary Serializer Persistent State Manager is a very basic implementation of the IPersistentStateManager interface that uses a Binary Reader and Writer to perform the saving and loading of data.

Creating A Binary Serializer Persistent State Manager Component

You can add this component by selecting a game object and then selecting from the Top Menu Bar:

Component -> Deep Space Labs -> -> SAM -> Persistent State Managers -> Binary Serializer Persistent State Manager

Adding A Binary Serializer Persistent State Manager Component From The Top Menu Bar GIF

or by selecting a game object and using the Add Component menu via its inspector:

Component -> Deep Space Labs -> -> SAM -> Persistent State Managers -> Binary Serializer Persistent State Manager

Adding A Binary Serializer Persistent State Manager Component From Add Component Menu GIF

Using the Binary Persistent State Manager

The Binary Persistent State Manager does not operate on its own; you must create some other script that manually calls the Component Manager's Save and Load methods. When you call these methods, you can pass in a reference to the manager component. From there, the Component Manager can handle everything, calling the appropriate methods of the manager in order to save/load data.

If the only persistent data you have to save/load is the persistent data belonging to the Streamable Assets Manager, then there is not much else to do besides specifying a File Name for the save game file.

If you would like to save and load other data using the Binary Persistent State Manager, that is perfectly fine! However, you must ensure that the order in which your data is loaded is the same as the order in which your data is saved. Additionally, you will likely need to enable the two toggle options in the inspector, and then call the OnBeforeDataSaved, OnAfterDataSaved, OnBeforeDataLoaded, and OnAfterDataLoaded methods manually. This is explained in more detail below.

Disable Auto Invocation Of Before And After Save Methods

By default (toggle disabled), when the Component Manager's Save method is called, it will invoke the OnBeforeDataSaved method before it begins the save process (the save process involves calling SaveInt, SaveFloat, etc.), and then OnAfterDataSaved once it finishes the save process.

In situations where the only data to save is SAM's persistent data, this strategy is optimal, as it automates thing which generally makes things easier for you (the developer).

On the other hand, if you have data unrelated to SAM that you wish to save using the state manager, it may be more efficient to have OnBeforeDataSaved called before the Component Manager's Save method, and OnAfterDataSaved after. In these situations, you can disable the auto invocation of the methods by enabling this option in the inspector.

Note, however, that you will need to manually call these methods yourself, before the Component Manager's Save method is called (in the case of OnBeforeDataSaved), and after (in the case of OnAfterDataSaved). Otherwise the saving process will not work correctly.

Disable Auto Invocation Of Before And After Load Methods

By default (toggle disabled), when the Component Manager's Load method is called, it will invoke the OnBeforeDataLoad method before it begins the load process (the load process involves calling LoadInt, LoadFloat, etc.), and then OnAfterDataSLoad once it finishes the load process.

In situations where the only data to load is SAM's persistent data, this strategy is optimal, as it automates thing which generally makes things easier for you (the developer).

On the other hand, if you have data unrelated to SAM that you wish to load using the state manager, it may be more efficient to have OnBeforeDataLoad called before the Component Manager's Load method, and OnAfterDataLoad after. In these situations, you can disable the auto invocation of the methods by enabling this option in the inspector.

Note, however, that you will need to manually call these methods yourself, before the Component Manager's Load method is called (in the case of OnBeforeDataLoad), and after (in the case of OnAfterDataLoad). Otherwise the loading process will not work correctly.

File Name

The file name set in the inspector will be the file name used to save the persistent data, unless you manually change the File Name at runtime via the FileName property. The file name must include an extension!

Where Is The Data Saved

The Data is saved in the file named File Name, which is always located at Application.persistentDataPath. You can view more information about that path (which is dependent on the Platform) on Unity's website:

Application.persistentDataPath Info

You cannot save the data in a different location. If you need to use a different location, you will have to create a custom Persistent State Manager.

Delete Save Data In The Editor

The Context Menu for the manager includes a handy method that when invoked, deletes the data for whatever File Name is set in the inspector (if it exist). This is useful for testing your save/load solution in the editor.

To access the context menu, simply right click over the area where the script name is located, or click the 3 dots in the upper right of the scripts inspector.

A Few Handy Properties

The Binary Serializer class includes two helpful properties for retrieving information about whether the Save Data file current exists (FileExist) and the full file path (including the file name) of the save data (FilePath).