using UnityEngine; namespace NBF.Setting { public interface IOptionBase { string Name { get; } void Initialize(ISettings root); void Apply(); int GetValue(); void SetValue(int index); bool HaveNotApple(); ISettings Root { get; } } public abstract class OptionBase : IOptionBase { protected string SaveKey => $"Setting_{Group}_{Name}"; public abstract string Name { get; } /// /// 所在组 /// public abstract string Group { get; } /// /// 默认值 /// protected abstract int DefaultValue { get; } /// /// 当前的值 /// protected int Value; /// /// 保存的值 /// protected int SaveValue; public bool HaveNotApple() { return !Value.Equals(SaveValue); } public ISettings Root { get; private set; } public void Initialize(ISettings root) { Root = root; OnInitialize(); Load(); Apply(); } /// /// 加载用户的设置 /// public void Load() { var value = PlayerPrefs.GetInt(SaveKey, DefaultValue); Value = value; SaveValue = value; } public void Apply() { PlayerPrefs.SetInt(SaveKey, Value); SaveValue = Value; OnApply(); } public void Reset() { Value = DefaultValue; } public virtual int GetValue() { return Value; } public virtual void SetValue(int value) { Value = value; } protected virtual void OnInitialize() { } protected abstract void OnApply(); } }