Runtime Save & Load

You can find the save/load settings in the Databrain editor.

To serialize your data to a Json file, you can simply call the Save method on the DataLibrary object and pass a path including save file as parameter. The same goes for loading the data back. Please see the following example code:

public class SaveLoadManager : MonoBehaviour
{
    public DataLibrary data;
    public string filename = "savegame.json";
    
    void Start()
    {
        data.RegisterInitializationCallback(Ready);
    }
    
    void Ready()
    {
        data.OnLoaded += DataLoaded;
        data.OnSaved += DataSaved;
    }
    
    // Call this method to save the data
    public void Save()
    {
        // Create save path
        var path = System.IO.Path.Combine(Application.persistentDataPath, filename);
        // Save data
         data.Save(path);   
    }
    
    // Call this method to load data back
    public void Load()
    {
        // Create save path
        var path = System.IO.Path.Combine(Application.persistentDataPath, filename);
        // Load data
        data.Load(path);
    }
    
    // Called after data has been loaded
    void DataLoaded()
    {
        Debug.Log("Data loaded");
        
        // Do something with it
    }
    
    // Called after data has been saved
    void DataSaved()
    {
        Debug.Log("Data saved");
    }
}

Last updated