Update your settings
Rename a field or move a class
Use Unity's own [FormerlySerializedAs] and [MovedFrom] attributes; they work here as
everywhere else. For changes they can't express, Settings Kit has two hooks, described below. Both run in the
editor when settings load, so builds always get already-updated values.
Convert saved values
Say version 1 stored int volume from 0 to 100, and version 2 stores
float masterVolume from 0 to 1. Implement IVersionedSettings, keep the old field
under its old name so its saved value still loads, and raise Version:
[System.Serializable]
public sealed class AudioDefaults : BuildSettingsEntry<GamePage>, IVersionedSettings
{
[SerializeField, Range(0f, 1f)] float masterVolume = 0.8f;
// The version 1 field, under its old name so its saved value still loads.
[SerializeField, HideInInspector] int volume = -1;
public int Version => 2;
Then convert the old value in Migrate:
public void Migrate(int fromVersion)
{
if (fromVersion < 2 && volume >= 0)
{
masterVolume = Mathf.Clamp01(volume / 100f);
volume = -1;
}
}
}
Migrate runs once, the first time older data loads. Each time you raise Version again, add an if (fromVersion < n) block below the previous ones.Recover a deleted class
Say you replace a NetworkSettings class with NetworkConfig. In any class of your
project, declare a struct with the old fields you want back. It doesn't need all of them:
public static class LegacySettingsMigrator
{
[System.Serializable]
struct OldNetworkSettings
{
public string address;
public int portNumber;
}
Then add a static method marked [SettingsMigrator]. It receives every saved setting whose class no
longer exists: recognize yours by its name, read it with TryRead, and copy the values into your
current settings.
[SettingsMigrator]
static void RecoverNetworkSettings(MissingSettings missing)
{
if (missing.ClassName != "NetworkSettings"
|| missing.Namespace != typeof(NetworkConfig).Namespace
|| missing.AssemblyName != typeof(NetworkConfig).Assembly.GetName().Name)
return;
if (missing.TryRead(out OldNetworkSettings old) == false)
return;
NetworkConfig current = Settings<NetworkConfig>.Instance;
// ... copy old.address / old.portNumber into current ...
Settings.Save();
}
}
static void with a single MissingSettings parameter, at any access level. A method with another signature is ignored, with an error in the console as soon as Unity compiles it.[SettingsMigrator] method before you delete the old class, or its values are lost the next time settings load.