SaveAsync

Summary: Save data by name and await the result.

Description

Save data by name and await the result.

Call SaveAsync from Unity’s main thread. Make your first Save System call on that thread too. Save System serializes the data on the main thread before queuing the write. Storage that requires the main thread, such as PlayerPrefs, also writes there.

Declaration

C#
public static Task<SaveResult> SaveAsync<T>(T data, string name, SaveOptions options = null);

Parameters

ParameterTypeDescription
T—Type of the data to save.
dataTThe data to save.
namestringName of the save.
optionsSaveOptionsOptional. Slot, protection and compression settings. Null uses the defaults.

Returns

Await Task<SaveResult> to get the save result. Check IsSuccess on the result.

Example

Create SavePositionAsync.cs and attach it to an active GameObject. Enter Play mode to save the GameObject’s position once. Check the Console for the result.

C#
using Gley.SaveSystem;
using UnityEngine;

public class SavePositionAsync : MonoBehaviour
{
    private async void Start()
    {
        SaveResult result = await API.SaveAsync(transform.position, "position-async-example");

        if (result.IsSuccess)
        {
            Debug.Log("Saved the GameObject's position.", this);
        }
        else
        {
            Debug.LogWarning($"Save failed ({result.Status}): {result.Message}", this);
        }
    }
}

To read the position back, use API.Load<Vector3>("position-async-example"). See the Load reference.

On This Page