AddUpgradeStep

Summary: Register a step that upgrades older data during load.

Description

Register a step that upgrades older data during load.

Declaration

C#
public static void AddUpgradeStep(ISaveUpgradeStep step);

Parameters

ParameterTypeDescription
stepISaveUpgradeStepThe upgrade step to add.

Exceptions

ExceptionThrown when
ArgumentNullExceptionstep is null.
ArgumentExceptionThe step has no target type, targets a type that can’t be versioned (an array, primitive, string, enum or System generic type), has a FromVersion below 1 or not below the type’s current SaveVersion, or repeats a step already registered for that type and version.
InvalidOperationExceptionThe method is called from a thread other than Unity’s main thread.

Example

Create RegisterPointsUpgrade.cs and attach it to an active GameObject. This example uses JsonUpgradeStep<T> with the optional Newtonsoft JSON package (com.unity.nuget.newtonsoft-json). Enter Play mode to register an upgrade for version 1 saves of the example data type. When such a save loads, the step renames _coins to _points before the version 2 data is read.

C#
using System;
using Gley.SaveSystem;
using Gley.SaveSystem.Newtonsoft;
using Newtonsoft.Json.Linq;
using UnityEngine;

public class RegisterPointsUpgrade : MonoBehaviour
{
    private void Awake()
    {
        API.AddUpgradeStep(new JsonUpgradeStep<UpgradeData>(1, RenameCoins));
        Debug.Log("Registered the upgrade from version 1 to version 2.", this);
    }

    private void RenameCoins(JObject data)
    {
        JToken coins = data["_coins"];
        if (coins != null)
        {
            data["_points"] = coins;
            data.Remove("_coins");
        }
    }

    [Serializable, SaveVersion(2)]
    private class UpgradeData
    {
        [SerializeField] private int _points = 0;

        public int Points => _points;
    }
}
On This Page