LoadFromString

Summary: Load data from text produced by SaveToString.

Description

Load data from text produced by SaveToString.

Declaration

C#
public static LoadResult<T> LoadFromString<T>(string text, SaveOptions options = null, Func<T> defaultValue = null);

Parameters

ParameterTypeDescription
T—Type of the data to load.
textstringThe text created by SaveToString.
optionsSaveOptionsReserved. Pass null. The saved text supplies its own protection and compression information.
defaultValueFunc<T>Optional. Creates the data to return when the text cannot be loaded.

Returns

A LoadResult<T> with the result of reading the text. Check IsSuccess before using Data.

Example

Create RestorePositionFromText.cs and attach it to an active GameObject. Copy the save text produced by the SaveToString example. Paste it into the component’s Save Text field before entering Play mode.

C#
using Gley.SaveSystem;
using UnityEngine;

public class RestorePositionFromText : MonoBehaviour
{
    [SerializeField, TextArea] private string _saveText = "";

    private void Start()
    {
        if (string.IsNullOrWhiteSpace(_saveText))
        {
            Debug.LogWarning("Paste the save text into the Save Text field.", this);
            return;
        }

        LoadResult<Vector3> result = API.LoadFromString<Vector3>(_saveText);

        if (result.IsSuccess)
        {
            transform.position = result.Data;
        }
        else
        {
            Debug.LogWarning($"Load failed ({result.Status}): {result.Message}", this);
        }
    }
}
On This Page