Files
ShrinkTutorial/Runtime/Storage/PlayerPrefsShrinkTutorialStorage.cs
T
cneicy 1180be88a6
Publish UPM package / publish (push) Failing after 1s
chore: initialize standalone UPM package
2026-08-26 02:50:43 +08:00

42 lines
1.2 KiB
C#

using UnityEngine;
namespace ShrinkTutorial
{
public sealed class PlayerPrefsShrinkTutorialStorage : IShrinkTutorialStorage
{
private readonly string _storageKey;
public PlayerPrefsShrinkTutorialStorage(string storageKey)
{
_storageKey = string.IsNullOrWhiteSpace(storageKey)
? "ShrinkTutorial.Progress"
: storageKey;
}
public ShrinkTutorialProgress Load()
{
if (!PlayerPrefs.HasKey(_storageKey))
return new ShrinkTutorialProgress();
var json = PlayerPrefs.GetString(_storageKey, string.Empty);
if (string.IsNullOrWhiteSpace(json))
return new ShrinkTutorialProgress();
return JsonUtility.FromJson<ShrinkTutorialProgress>(json) ?? new ShrinkTutorialProgress();
}
public void Save(ShrinkTutorialProgress progress)
{
var json = JsonUtility.ToJson(progress ?? new ShrinkTutorialProgress());
PlayerPrefs.SetString(_storageKey, json);
PlayerPrefs.Save();
}
public void Reset()
{
PlayerPrefs.DeleteKey(_storageKey);
PlayerPrefs.Save();
}
}
}