using UnityEngine; namespace Ubisoft { /// /// Base class for all HtPlayerPrefs implementations that need to persist data using UnityEngine.PlayerPrefs /// public abstract class HtPlayerPrefs_Unity_Base : HtPlayerPrefs { private static bool s_needsToSave = false; internal HtPlayerPrefs_Unity_Base(string channel, HtLogger.Channel logger = null, bool isVerbose = false) : base(channel, logger, isVerbose) { } public override bool NeedsToSave { get { return base.NeedsToSave; } set { base.NeedsToSave = value; // ExtendedSave method in this class calls UnityEngine.PlayerPrefs.Save(), which stores all Unity PlayerPrefs regardless of which HtUnityPrefs instance needs to be saved. // We use a static property to make sure that PlayerPrefs.Save() will only get called once for all instances. if (value) { s_needsToSave = true; } } } protected override void ExtendedSave() { if (s_needsToSave) { s_needsToSave = false; if (IsVerbose) { Logger.LogTrace("UnityEngine.PlayerPrefs.Save()"); } PlayerPrefs.Save(); } } protected int BoolToInt(bool value) { return value ? 1 : 0; } protected bool IntToBool(int value) { return value == 1; } protected string LongToString(long value) { return value.ToString(); } protected bool StringToLong(string key, string value, out long parsedValue) { if (!HtLong.TryParse(value, out parsedValue)) { Logger.LogError($"Error parsing {Debug.FormatTextInUserContext(value)} for key {key}: Expected a long value."); return false; } return true; } } }