using System; using System.Collections.Generic; using System.Linq; using UnityEngine; public abstract class AxiPlayerPrefsFileBase : IAxiPlayerPrefs { protected static string AxiPlayerPrefsFilePath => AxiPlayerPrefs.SaveDataRootDirPath + "/AxiPlayerPrefs.dat"; Dictionary m_keyval = new Dictionary(); Func> m_LoadFunc; Action> m_SaveFunc; bool bDirty = false; [Serializable] public class AxiPlayerPrefsAllData { public int version; public List datalist; } [Serializable] public class AxiPlayerPrefsKeyValye { public string key; public int intval; public string strval; public float floatval; } public AxiPlayerPrefsFileBase(Func> load, Action> save) { m_LoadFunc = load; m_SaveFunc = save; Load(); AxiPlayerPrefsMono.SetInvoke(Save, 15); } public static Dictionary JsonStrToData(string dataStr) { AxiPlayerPrefsAllData alldata = UnityEngine.JsonUtility.FromJson(dataStr); Dictionary data = new Dictionary(); foreach (var item in alldata.datalist) { data.Add(item.key, item); } return data; } public static string DataToJsonStr(Dictionary data) { return UnityEngine.JsonUtility.ToJson(new AxiPlayerPrefsAllData() { version = 1, datalist = data.Values.ToList() }); } AxiPlayerPrefsKeyValye GetByKey(string key, bool NonAutoCreate, out bool IsNew) { //Debug.Log($"GetByKey=>{key}"); if (!m_keyval.ContainsKey(key)) { IsNew = true; if (!NonAutoCreate) return null; m_keyval.Add(key, new AxiPlayerPrefsKeyValye() { key = key }); } else IsNew = false; return m_keyval[key]; } public void Load() { m_keyval = m_LoadFunc.Invoke(); } public void Save() { if (bDirty) { Debug.Log("Auto AxiPlayerPrefs."); bDirty = false; m_SaveFunc.Invoke(m_keyval); } } public float GetFloat(string key, float defaultValue) { AxiPlayerPrefsKeyValye kv = GetByKey(key, true, out bool IsNew); if (IsNew) kv.floatval = defaultValue; return kv.floatval; } public int GetInt(string key, int defaultValue) { AxiPlayerPrefsKeyValye kv = GetByKey(key, true, out bool IsNew); if (IsNew) kv.intval = defaultValue; return kv.intval; } public string GetString(string key, string defaultValue) { AxiPlayerPrefsKeyValye kv = GetByKey(key, true, out bool IsNew); if (IsNew) kv.strval = defaultValue; return kv.strval; } public float GetFloat(string key) { AxiPlayerPrefsKeyValye kv = GetByKey(key, false, out bool _); if (kv != null) return kv.floatval; return default(float); } public int GetInt(string key) { AxiPlayerPrefsKeyValye kv = GetByKey(key, false, out bool _); if (kv != null) return kv.intval; return default(int); } public string GetString(string key) { AxiPlayerPrefsKeyValye kv = GetByKey(key, false, out bool _); if (kv != null) return kv.strval; return string.Empty; } public void SetInt(string key, int value) { AxiPlayerPrefsKeyValye kv = GetByKey(key, true, out bool _); if (kv.intval == value) return; kv.intval = value; bDirty = true; } public void SetString(string key, string value) { AxiPlayerPrefsKeyValye kv = GetByKey(key, true, out bool _); if (string.Equals(kv.strval, value)) return; kv.strval = value; bDirty = true; } public void SetFloat(string key, float value) { AxiPlayerPrefsKeyValye kv = GetByKey(key, true, out bool _); if (kv.floatval == value) return; kv.floatval = value; bDirty = true; } public void DeleteAll() { m_keyval.Clear(); bDirty = true; } }