Getting Started
Woohoo, so let's get started with saving and loading your data!
If you're coming too early here, make sure to follow the Installation article before proceeding.
Check out the Demos while going through this guide for more practical learning.
Types of Data
There are different types of data that you need to know about before learning how to save your data, here are a few of the most important ones:
- Referenced (Unity objects): it includes a broad range of types such as Game Objects, Components, Materials, Custom ScriptableObjects, ... or anything that comes down to Unity object category
- Non-referenced (Non-Unity objects): any other kind of data and objects, such as Custom classes, Primitive data (integer, float, decimal, ...)
The difference here is that Unity objects are referenced by Save System using an internal GUID assigned to each Unity object used for serialization because some of the Unity objects cannot be created at run-time and fill them with the persistent data, so for doing that, the Save System serializes the GUID of the corresponding object for example a Material and then serializes the Material properties underneath, so in the deserialization or loading phase, the Save System uses the database that contains all the GUIDs and the references to the objects to find that object or Material using the provided GUID and then loads the saved data into that object or Material in this example.
Unity Objects
To save Unity objects you need to setup the Save System Manager in your scene, you can do it by right clicking on the scene hierarchy and choosing the Bayat > Save System > Save System Manager menu:
After doing so, a new Game Object called Save System Manager will be created that has a Save System Manager, Auto Save Manager and Scene Reference Resolver component.
Non-Unity Objects
There are no prerequisites for saving non-unity objects, you can save them right away after installing the Save System properly without even adding the Save System Manager to the scene by using the Save System API, but if any of your data includes a reference to a Unity object, then you'll have to setup the Unity objects prerequisites before proceeding.
Common ways to Save the Data
You can save your data either using the Save System API manually or using the built-in Auto Save feature to save your scene objects.
Auto Save
Using the Auto Save feature you can specify a bunch of objects to save their components and information by adding the Auto Save component to your desired objects and then adding an Auto Save Manager to the scene for managing the Save and Load events and configuring the save process:
The Auto Save components lets you specify which components and children to be serialized and which ones to be excluded if you are serializing all components, you can also invert this behaviour, that means the excluded list will be serialized instead of being excluded by using either of the Serialize Excluded Chilren or Serialize Excluded Components toggles.
Also keep in mind that if you set the Save or Load event of the Auto Save Manager to manual, you'll have to call the Save
or Load
method of the Auto Save Manager manually or using an external event, for example using a button click event:
Or using a custom script by accessing the AutoSaveManager.Current
property and calling the Save
method:
public class CustomScript : MonoBehaviour {
void Start () {
Load();
}
void OnApplicationQuit () {
Save();
}
public void Save () {
AutoSaveManager.Current.Save();
}
public void Load () {
AutoSaveManager.Current.Load();
}
}
Learn more about the Auto Save feature >
Manual
You can save and load your data easily using the Save System API in your custom script, but it requires an intermediate programming knowledge.
Here is a simple example of saving a primitive data:
async void Start() {
// The location in the storage that we want to save the data, it can be a file, a key, or any kind of identification for the type of storage
string identifier = "name.json";
// The simple data we're going to save
string data = "Bayat - Save System";
// Save the data associated with the identifier
await SaveSystemAPI.SaveAsync(identifier, data);
// Load back the saved data associated with the identifier
string loadedData = await SaveSystemAPI.LoadAsync<string>(identifier);
Debug.Log(data); // Outputs "Bayat - Save System"
Debug.Log(loadedData); // Outputs "Bayat - Save System"
}
As you can see in the example above, the Save System API uses Async/Await pattern, so you'll need to add an async
keyword behind your method before peforming the API.
Learn more about the Save System API >
Check out Guides for more examples and detailed walkthroughs