diff --git a/Assets/00.StaryEvo/Runtime/Tool/CustomPlayerPrefs.cs b/Assets/00.StaryEvo/Runtime/Tool/CustomPlayerPrefs.cs index 8d6b150..0e181b9 100644 --- a/Assets/00.StaryEvo/Runtime/Tool/CustomPlayerPrefs.cs +++ b/Assets/00.StaryEvo/Runtime/Tool/CustomPlayerPrefs.cs @@ -2,13 +2,12 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; -using UnityEditor; using UnityEngine; namespace Stary.Evo { /// - /// 自定义EditorPrefs实现,数据存储在项目根目录的EditorPrefs.ini文件中 + /// 自定义PlayerPrefs实现,数据存储在项目根目录的PlayerPrefs.ini文件中 /// public static class CustomPlayerPrefs { @@ -26,9 +25,17 @@ namespace Stary.Evo /// 值 public static void SetString(string key, string value) { - LoadData(); - _dataCache[key] = value; - SaveData(); + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + PlayerPrefs.SetString(key, value); + PlayerPrefs.Save(); + } + else + { + LoadData(); + _dataCache[key] = value; + SaveData(); + } } /// @@ -39,9 +46,16 @@ namespace Stary.Evo /// public static string GetString(string key, string defaultValue = "") { - LoadData(); - _dataCache.TryGetValue(key, out var value); - return value is string ? (string)value : defaultValue; + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + return PlayerPrefs.GetString(key, defaultValue); + } + else + { + LoadData(); + _dataCache.TryGetValue(key, out var value); + return value is string ? (string)value : defaultValue; + } } /// @@ -51,9 +65,17 @@ namespace Stary.Evo /// 值 public static void SetInt(string key, int value) { - LoadData(); - _dataCache[key] = value; - SaveData(); + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + PlayerPrefs.SetInt(key, value); + PlayerPrefs.Save(); + } + else + { + LoadData(); + _dataCache[key] = value; + SaveData(); + } } /// @@ -64,8 +86,15 @@ namespace Stary.Evo /// public static int GetInt(string key, int defaultValue = 0) { - LoadData(); - return _dataCache.TryGetValue(key, out var value) ? (int)value : defaultValue; + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + return PlayerPrefs.GetInt(key, defaultValue); + } + else + { + LoadData(); + return _dataCache.TryGetValue(key, out var value) ? (int)value : defaultValue; + } } /// @@ -75,9 +104,17 @@ namespace Stary.Evo /// 值 public static void SetFloat(string key, float value) { - LoadData(); - _dataCache[key] = value; - SaveData(); + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + PlayerPrefs.SetFloat(key, value); + PlayerPrefs.Save(); + } + else + { + LoadData(); + _dataCache[key] = value; + SaveData(); + } } /// @@ -88,8 +125,15 @@ namespace Stary.Evo /// public static float GetFloat(string key, float defaultValue = 0.0f) { - LoadData(); - return _dataCache.TryGetValue(key, out var value) ? (float)value : defaultValue; + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + return PlayerPrefs.GetFloat(key, defaultValue); + } + else + { + LoadData(); + return _dataCache.TryGetValue(key, out var value) ? (float)value : defaultValue; + } } /// @@ -99,9 +143,17 @@ namespace Stary.Evo /// 值 public static void SetBool(string key, bool value) { - LoadData(); - _dataCache[key] = value; - SaveData(); + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + PlayerPrefs.SetInt(key, value ? 1 : 0); + PlayerPrefs.Save(); + } + else + { + LoadData(); + _dataCache[key] = value; + SaveData(); + } } /// @@ -112,8 +164,15 @@ namespace Stary.Evo /// public static bool GetBool(string key, bool defaultValue = false) { - LoadData(); - return _dataCache.TryGetValue(key, out var value) ? (bool)value : defaultValue; + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + return PlayerPrefs.GetInt(key, defaultValue ? 1 : 0) == 1; + } + else + { + LoadData(); + return _dataCache.TryGetValue(key, out var value) ? (bool)value : defaultValue; + } } /// @@ -123,8 +182,15 @@ namespace Stary.Evo /// 是否存在 public static bool HasKey(string key) { - LoadData(); - return _dataCache.ContainsKey(key); + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + return PlayerPrefs.HasKey(key); + } + else + { + LoadData(); + return _dataCache.ContainsKey(key); + } } /// @@ -133,10 +199,18 @@ namespace Stary.Evo /// 键 public static void DeleteKey(string key) { - LoadData(); - if (_dataCache.Remove(key)) + if (Application.platform == RuntimePlatform.WebGLPlayer) { - SaveData(); + PlayerPrefs.DeleteKey(key); + PlayerPrefs.Save(); + } + else + { + LoadData(); + if (_dataCache.Remove(key)) + { + SaveData(); + } } } @@ -145,8 +219,16 @@ namespace Stary.Evo /// public static void DeleteAll() { - _dataCache.Clear(); - SaveData(); + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + PlayerPrefs.DeleteAll(); + PlayerPrefs.Save(); + } + else + { + _dataCache.Clear(); + SaveData(); + } } /// @@ -155,8 +237,17 @@ namespace Stary.Evo /// 键列表 public static string[] GetAllKeys() { - LoadData(); - return _dataCache.Keys.ToArray(); + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + // WebGL平台无法直接获取所有键,这里返回空数组 + // 注意:实际项目中可能需要维护一个键的列表 + return new string[0]; + } + else + { + LoadData(); + return _dataCache.Keys.ToArray(); + } } /// @@ -165,8 +256,17 @@ namespace Stary.Evo /// 键值对字典 public static Dictionary GetAll() { - LoadData(); - return new Dictionary(_dataCache); + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + // WebGL平台无法直接获取所有键值对,这里返回空字典 + // 注意:实际项目中可能需要维护一个键值对的字典 + return new Dictionary(); + } + else + { + LoadData(); + return new Dictionary(_dataCache); + } } /// @@ -174,6 +274,12 @@ namespace Stary.Evo /// private static void LoadData() { + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + // WebGL平台使用PlayerPrefs,不需要从文件加载 + return; + } + if (_isDataLoaded && _dataCache != null) return; @@ -234,6 +340,12 @@ namespace Stary.Evo /// private static void SaveData() { + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + // WebGL平台使用PlayerPrefs,不需要保存到文件 + return; + } + if (_dataCache == null) return; @@ -272,6 +384,12 @@ namespace Stary.Evo /// public static void Refresh() { + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + // WebGL平台使用PlayerPrefs,不需要刷新 + return; + } + _isDataLoaded = false; LoadData(); } diff --git a/Assets/00.StaryEvo/package.json b/Assets/00.StaryEvo/package.json index ab5f741..5a7490d 100644 --- a/Assets/00.StaryEvo/package.json +++ b/Assets/00.StaryEvo/package.json @@ -1,6 +1,6 @@ { "name": "com.staryevo.main", - "version": "2.1.13", + "version": "2.1.14", "displayName": "00.StaryEvo", "description": "This is an Framework package(后台服务器版本,端口9527)", "unity": "2021.3",