Comment on page
- 1.Implement the ISaveable interface to your script, then you will need to define the interface methods in your script.
- 2.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.
- 3.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.
- 1.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.
- 2.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.
- 3.Open the Object References asset, which can be found in the Save Game Manager component.
- 3.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.
- 4.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.
public class RuntimeSaveableTest : MonoBehaviour
public ObjectReference ObjectReference;
private void Start()
GameObject obj = SaveGameManager.InstantiateSaveable(ObjectReference, Vector3.zero, Vector3.zero);
// do something with instantiated game object
- 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.