SetStorage

Summary: Switch to a different storage backend.

Description

Switch to a different storage backend. Later saves, loads, deletes and slot operations on this service use the new backend. Pending work on the previous storage finishes first. The example uses MemoryStorage, which lasts only for the current session.

Declaration

C#
public static void SetStorage(ISaveStorage storage);

Parameters

ParameterTypeDescription
storageISaveStorageThe storage to use from now on.

Exceptions

ExceptionThrown when
ArgumentNullExceptionstorage is null.
InvalidOperationExceptionThe method is called from a thread other than Unity’s main thread.

Example

Create UseMemorySaveStorage.cs and attach it to an active GameObject. Use this example in a test scene. Enter Play mode to switch the shared service to memory storage before other components call Save or Load from Start. Saves written to memory disappear when the session ends.

C#
using Gley.SaveSystem;
using UnityEngine;

public class UseMemorySaveStorage : MonoBehaviour
{
    private void Awake()
    {
        API.SetStorage(new MemoryStorage("docs-memory-example"));
        Debug.Log("Save System is using memory storage.", this);
    }
}
On This Page