SaveVersionAttribute

Summary: Mark the data version of a serializable class or struct.

Description

Apply SaveVersion to the type you pass to Save or Load. Versions start at 1. If the type has no attribute, Save System uses version 1. Increase the version when your data format changes and provide an upgrade where needed.

Namespace Gley.SaveSystem.

Declaration

C#
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
public sealed class SaveVersionAttribute : Attribute

Members

MemberTypeDescription
SaveVersionAttributeConstructorSaveVersionAttribute(int version)
Sets the version. Values below 1 throw ArgumentOutOfRangeException.
VersionintThe version written for the data type.

Example

Create SaveVersionedProgress.cs, attach it to a GameObject and enter Play mode. The save stores version 1 for the Progress data type.

C#
using System;
using Gley.SaveSystem;
using UnityEngine;

public class SaveVersionedProgress : MonoBehaviour
{
    private void Start()
    {
        Progress data = new Progress();
        SaveResult result = API.Save(data, "versioned-progress");
        Debug.Log($"Save status: {result.Status}", this);
    }

    [Serializable, SaveVersion(1)]
    private sealed class Progress
    {
        [SerializeField] private int _level = 1;

        public int Level => _level;
    }
}
On This Page