Save/Load Manager
Last updated
Last updated
Implement the ISaveable interface to your script, then you will need to define the interface methods in your script.
Specify what should be saved in the OnSave() method and what should be loaded in the OnLoad() method.
The StorableCollection is essentially a Dictionary that uses a string as the key and an object as the value. The object can also be a new StorableCollection that contains additional values.
Here are the data types that can be serialized by Newtonsoft.Json in Unity:
All primitive types, such as int, float, double, bool, and string.
Arrays and lists of primitive types.
Custom classes that have public properties or fields, as long as they do not contain circular references.
Dictionaries that have string keys and values of primitive types or custom classes that meet the above criteria.
Go to GAMEMANAGER -> Save Game Manager and select the Find Saveables button. This will automatically locate all components in the scene that have implemented the ISaveable interface.
Don't forget to SAVE your scene after locating all saveables.
This is necessary because finding saveables creates a new GUID for each component, which is used to serialize the data. When you load the game, the GUID is used to locate a reference to a specific component. If you don't save the scene, it won't be able to locate the correct GUID reference.
If a referenced saveable is removed from the scene, its GUID will still be stored in the saveables list. It's a good to use the Find Saveables button again to recreate the saveables list and avoid any issues during the serialization process.
By using the InspectorHeader attribute in your script, you can change the header of the script to the same fancy header used in almost all scripts in the UHFPS. This header also indicates which scripts are saveable, making it easier to keep track of them without opening the script editor.
Change the script to inherit from SaveableBehaviour. The SaveableBehaviour class creates a UniqueID that is used with SaveGameManager to identify which data belongs to which component within the object.
Override the OnSave() and OnLoad() methods and specify which data you want to save or load in the same way as you would when using the ISaveable interface.
Open the Object References asset, which can be found in the Save Game Manager component.
Add your Prefab, which contains a script that inherits from SaveableBehaviour, to the Object References list by dragging the Prefab or Folder containing the Prefabs to the window.
Adding a Prefab to the Object References will create a GUID that is used with the Save Game Manager to instantiate the object when the game is loaded at runtime.
The Interactable Item component already inherits from SaveableBehaviour, so when you drop the item from the inventory, it will be automatically added to the runtime saveables.
To instantiate a saveable object at runtime, you must use the SaveGameManager.InstantiateSaveable method. When using this method, the manager will automatically locate the saveables within the object and add them to the runtime saveables.
To specify which object reference to instantiate, use the ObjectReference field. This field is similar to a GameObject field, but instead of providing a direct reference to the GameObject, you provide the GUID to the object reference.