引入AxiPlayerPrefs,替换现有PlayerPrefs,使用文件系统,替代不同平台的PlayerPrefs读写
This commit is contained in:
parent
6193e57a99
commit
7b40e190d0
@ -0,0 +1,44 @@
|
|||||||
|
using AxibugEmuOnline.Client.ClientCore;
|
||||||
|
|
||||||
|
public static class AxiPlayerPrefs
|
||||||
|
{
|
||||||
|
//#if UNITY_SWITCH && !UNITY_EDITOR
|
||||||
|
// public static string SaveDataRootDirPath = "save:/axibug";
|
||||||
|
//#elif UNITY_PSP2 && !UNITY_EDITOR
|
||||||
|
// public static string SaveDataRootDirPath = "ux0:data/axibug";
|
||||||
|
//#else
|
||||||
|
// public static string SaveDataRootDirPath = UnityEngine.Application.persistentDataPath;
|
||||||
|
//#endif
|
||||||
|
|
||||||
|
//使用统一的平台宏区分目录
|
||||||
|
public static string SaveDataRootDirPath => App.PersistentDataRootPath();
|
||||||
|
|
||||||
|
static IAxiPlayerPrefs m_axiPlayerPrefs;
|
||||||
|
static IAxiPlayerPrefs axiPlayerPrefs
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (m_axiPlayerPrefs == null)
|
||||||
|
{
|
||||||
|
#if UNITY_SWITCH || UNITY_PSP2
|
||||||
|
m_axiPlayerPrefs = new AxiPlayerPrefsForFileSystem();
|
||||||
|
#else
|
||||||
|
m_axiPlayerPrefs = new AxiPlayerPrefsForUnity();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return m_axiPlayerPrefs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float GetFloat(string key) { return axiPlayerPrefs.GetFloat(key); }
|
||||||
|
public static void SetFloat(string key, float value) { axiPlayerPrefs.SetFloat(key, value); }
|
||||||
|
public static float GetFloat(string key, float defaultValue) { return axiPlayerPrefs.GetFloat(key, defaultValue); }
|
||||||
|
public static int GetInt(string key) { return axiPlayerPrefs.GetInt(key); }
|
||||||
|
public static void SetInt(string key, int value) { axiPlayerPrefs.SetInt(key, value); }
|
||||||
|
public static int GetInt(string key, int defaultValue) { return axiPlayerPrefs.GetInt(key, defaultValue); }
|
||||||
|
public static string GetString(string key) { return axiPlayerPrefs.GetString(key); }
|
||||||
|
public static void SetString(string key, string value) { axiPlayerPrefs.SetString(key, value); }
|
||||||
|
public static string GetString(string key, string defaultValue) { return axiPlayerPrefs.GetString(key, defaultValue); }
|
||||||
|
|
||||||
|
internal static void DeleteAll() { axiPlayerPrefs.DeleteAll(); }
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 71c6edecb09ea914b90fa115b491698d
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -0,0 +1,162 @@
|
|||||||
|
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<string, AxiPlayerPrefsKeyValye> m_keyval = new Dictionary<string, AxiPlayerPrefsKeyValye>();
|
||||||
|
Func<Dictionary<string, AxiPlayerPrefsKeyValye>> m_LoadFunc;
|
||||||
|
Action<Dictionary<string, AxiPlayerPrefsKeyValye>> m_SaveFunc;
|
||||||
|
bool bDirty = false;
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class AxiPlayerPrefsAllData
|
||||||
|
{
|
||||||
|
public int version;
|
||||||
|
public List<AxiPlayerPrefsKeyValye> datalist;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class AxiPlayerPrefsKeyValye
|
||||||
|
{
|
||||||
|
public string key;
|
||||||
|
public int intval;
|
||||||
|
public string strval;
|
||||||
|
public float floatval;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AxiPlayerPrefsFileBase(Func<Dictionary<string, AxiPlayerPrefsKeyValye>> load, Action<Dictionary<string, AxiPlayerPrefsKeyValye>> save)
|
||||||
|
{
|
||||||
|
m_LoadFunc = load;
|
||||||
|
m_SaveFunc = save;
|
||||||
|
Load();
|
||||||
|
AxiPlayerPrefsMono.SetInvoke(Save, 15);
|
||||||
|
}
|
||||||
|
public static Dictionary<string, AxiPlayerPrefsKeyValye> JsonStrToData(string dataStr)
|
||||||
|
{
|
||||||
|
AxiPlayerPrefsAllData alldata = UnityEngine.JsonUtility.FromJson<AxiPlayerPrefsAllData>(dataStr);
|
||||||
|
Dictionary<string, AxiPlayerPrefsKeyValye> data = new Dictionary<string, AxiPlayerPrefsKeyValye>();
|
||||||
|
foreach (var item in alldata.datalist)
|
||||||
|
{
|
||||||
|
data.Add(item.key, item);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string DataToJsonStr(Dictionary<string, AxiPlayerPrefsKeyValye> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 4085aa85cd82706448a25a172cd681a0
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class AxiPlayerPrefsForFileSystem : AxiPlayerPrefsFileBase
|
||||||
|
{
|
||||||
|
public AxiPlayerPrefsForFileSystem() : base(LoadData, SaveData)
|
||||||
|
{
|
||||||
|
Debug.Log($"AxiPlayerPrefsForPSVita Init");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Dictionary<string, AxiPlayerPrefsKeyValye> LoadData()
|
||||||
|
{
|
||||||
|
if (!AxiIO.AxiIO.io.file_Exists(AxiPlayerPrefsFilePath))
|
||||||
|
return new Dictionary<string, AxiPlayerPrefsKeyValye>();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string outputData = string.Empty;
|
||||||
|
byte[] loadedData = AxiIO.AxiIO.io.file_ReadAllBytes(AxiPlayerPrefsFilePath);
|
||||||
|
if (loadedData != null && loadedData.Length != 0)
|
||||||
|
{
|
||||||
|
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(loadedData))
|
||||||
|
{
|
||||||
|
using (System.IO.BinaryReader reader = new System.IO.BinaryReader(stream))
|
||||||
|
{
|
||||||
|
outputData = reader.ReadString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (string.IsNullOrEmpty(outputData))
|
||||||
|
return new Dictionary<string, AxiPlayerPrefsKeyValye>();
|
||||||
|
return AxiPlayerPrefsFileBase.JsonStrToData(outputData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SaveData(Dictionary<string, AxiPlayerPrefsKeyValye> data)
|
||||||
|
{
|
||||||
|
string jsonStr = AxiPlayerPrefsFileBase.DataToJsonStr(data);
|
||||||
|
byte[] dataByteArray;
|
||||||
|
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(jsonStr.Length * sizeof(char)))
|
||||||
|
{
|
||||||
|
System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(stream);
|
||||||
|
binaryWriter.Write(jsonStr);
|
||||||
|
dataByteArray = stream.GetBuffer();
|
||||||
|
stream.Close();
|
||||||
|
}
|
||||||
|
AxiIO.AxiIO.io.file_WriteAllBytes(AxiPlayerPrefsFilePath, dataByteArray, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class AxiPlayerPrefsForUnity : IAxiPlayerPrefs
|
||||||
|
{
|
||||||
|
public string GetString(string key, string defaultValue)
|
||||||
|
{
|
||||||
|
return PlayerPrefs.GetString(key, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetString(string key, string value)
|
||||||
|
{
|
||||||
|
PlayerPrefs.SetString(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetInt(string key, int defaultValue)
|
||||||
|
{
|
||||||
|
return PlayerPrefs.GetInt(key, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetInt(string key, int value)
|
||||||
|
{
|
||||||
|
PlayerPrefs.SetInt(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public float GetFloat(string key, float defaultValue)
|
||||||
|
{
|
||||||
|
return PlayerPrefs.GetFloat(key, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public float GetFloat(string key)
|
||||||
|
{
|
||||||
|
return PlayerPrefs.GetFloat(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetInt(string key)
|
||||||
|
{
|
||||||
|
return PlayerPrefs.GetInt(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetString(string key)
|
||||||
|
{
|
||||||
|
return PlayerPrefs.GetString(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetFloat(string key, float value)
|
||||||
|
{
|
||||||
|
PlayerPrefs.SetFloat(key,value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteAll()
|
||||||
|
{
|
||||||
|
PlayerPrefs.DeleteAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 94efbbb58793f1146b395c1ebfe19d33
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -0,0 +1,39 @@
|
|||||||
|
using System;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class AxiPlayerPrefsMono : MonoBehaviour
|
||||||
|
{
|
||||||
|
Action act;
|
||||||
|
float waittime;
|
||||||
|
float lastinvokeTime;
|
||||||
|
public static void SetInvoke(Action _act, int _waitsec)
|
||||||
|
{
|
||||||
|
GameObject gobj = GameObject.Find($"[{nameof(AxiPlayerPrefsMono)}]");
|
||||||
|
if (gobj == null)
|
||||||
|
{
|
||||||
|
gobj = new GameObject();
|
||||||
|
gobj.name = $"[{nameof(AxiPlayerPrefsMono)}]";
|
||||||
|
GameObject.DontDestroyOnLoad(gobj);
|
||||||
|
}
|
||||||
|
AxiPlayerPrefsMono com = gobj.GetComponent<AxiPlayerPrefsMono>();
|
||||||
|
if (com == null)
|
||||||
|
{
|
||||||
|
com = gobj.AddComponent<AxiPlayerPrefsMono>();
|
||||||
|
}
|
||||||
|
com.act = _act;
|
||||||
|
com.waittime = _waitsec;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnEnable()
|
||||||
|
{
|
||||||
|
Debug.Log("AxiPlayerPrefsMono Enable");
|
||||||
|
}
|
||||||
|
public void Update()
|
||||||
|
{
|
||||||
|
if (Time.time - lastinvokeTime < waittime)
|
||||||
|
return;
|
||||||
|
lastinvokeTime = Time.time;
|
||||||
|
if (act != null)
|
||||||
|
act.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 8fff109e9498ef549a7d124a24894a98
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
public interface IAxiPlayerPrefs
|
||||||
|
{
|
||||||
|
void DeleteAll();
|
||||||
|
float GetFloat(string key, float defaultValue);
|
||||||
|
float GetFloat(string key);
|
||||||
|
int GetInt(string key, int defaultValue);
|
||||||
|
int GetInt(string key);
|
||||||
|
string GetString(string key);
|
||||||
|
string GetString(string key, string defaultValue);
|
||||||
|
void SetFloat(string key, float value);
|
||||||
|
void SetInt(string key, int value);
|
||||||
|
void SetString(string key, string value);
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e194ccb9ac1dc984da8922b7c67ec879
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Loading…
Reference in New Issue
Block a user