From d6d2f66902b5085c3f87749f0d9f93db354304e0 Mon Sep 17 00:00:00 2001
From: Alex-Rachel <574809918@qq.com>
Date: Sat, 8 Mar 2025 14:05:41 +0800
Subject: [PATCH] Utility.PlayerPref
---
.../Core/Utility/Utility.PlayerPrefs.cs | 270 ++++++++++++++++++
.../Core/Utility/Utility.PlayerPrefs.cs.meta | 3 +
2 files changed, 273 insertions(+)
create mode 100644 UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.PlayerPrefs.cs
create mode 100644 UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.PlayerPrefs.cs.meta
diff --git a/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.PlayerPrefs.cs b/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.PlayerPrefs.cs
new file mode 100644
index 00000000..9e8adf88
--- /dev/null
+++ b/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.PlayerPrefs.cs
@@ -0,0 +1,270 @@
+namespace TEngine
+{
+ public static partial class Utility
+ {
+ ///
+ /// PlayerPrefs 增强工具类。
+ /// 功能特性:
+ /// 1. 支持全局启用/禁用 PlayerPrefs 功能
+ /// 2. 支持用户隔离存储(通过用户ID自动生成复合键)
+ /// 3. 扩展布尔值存储支持
+ ///
+ public static class PlayerPrefs
+ {
+ // 字段注释 -----------------------------------
+
+ ///
+ /// 全局开关标识(true时所有操作生效)。
+ ///
+ private static bool _enable = true;
+
+ ///
+ /// 当前用户标识(用于生成用户隔离的存储键)。
+ ///
+ private static string _userId = "";
+
+ // 基础方法注释 -------------------------------
+
+ ///
+ /// 删除所有 PlayerPrefs 数据。
+ ///
+ public static void DeleteAll()
+ {
+ if (!_enable) return;
+ UnityEngine.PlayerPrefs.DeleteAll();
+ }
+
+ ///
+ /// 删除指定键的存储数据。
+ ///
+ /// 要删除的数据键名。
+ public static void DeleteKey(string key)
+ {
+ if (!_enable) return;
+ UnityEngine.PlayerPrefs.DeleteKey(key);
+ }
+
+ // 数值获取方法 -------------------------------
+
+ ///
+ /// 获取浮点数值(未找到返回-1)。
+ ///
+ public static float GetFloat(string key)
+ {
+ return _enable ? UnityEngine.PlayerPrefs.GetFloat(key) : -1f;
+ }
+
+ ///
+ /// 获取浮点数值(带默认值)
+ ///
+ public static float GetFloat(string key, float defaultValue)
+ {
+ return _enable ? UnityEngine.PlayerPrefs.GetFloat(key, defaultValue) : -1f;
+ }
+
+ ///
+ /// 获取整数值(未找到返回-1)。
+ ///
+ public static int GetInt(string key)
+ {
+ return _enable ? UnityEngine.PlayerPrefs.GetInt(key) : -1;
+ }
+
+ ///
+ /// 获取整数值(带默认值)。
+ ///
+ public static int GetInt(string key, int defaultValue)
+ {
+ return _enable ? UnityEngine.PlayerPrefs.GetInt(key, defaultValue) : -1;
+ }
+
+ // 字符串获取方法 -----------------------------
+
+ ///
+ /// 获取字符串值(未找到返回空字符串)。
+ ///
+ public static string GetString(string key)
+ {
+ return _enable ? UnityEngine.PlayerPrefs.GetString(key) : string.Empty;
+ }
+
+ ///
+ /// 获取字符串值(带默认值)。
+ ///
+ public static string GetString(string key, string defaultValue)
+ {
+ return _enable ? UnityEngine.PlayerPrefs.GetString(key, defaultValue) : string.Empty;
+ }
+
+ ///
+ /// 检查指定键是否存在。
+ ///
+ public static bool HasKey(string key)
+ {
+ return _enable && UnityEngine.PlayerPrefs.HasKey(key);
+ }
+
+ // 数值设置方法 -------------------------------
+
+ ///
+ /// 设置浮点数值(自动保存)。
+ ///
+ public static void SetFloat(string key, float value, bool save = true)
+ {
+ if (!_enable) return;
+ UnityEngine.PlayerPrefs.SetFloat(key, value);
+ if (save)
+ {
+ UnityEngine.PlayerPrefs.Save();
+ }
+ }
+
+ ///
+ /// 设置整数值(自动保存)。
+ ///
+ public static void SetInt(string key, int value, bool save = true)
+ {
+ if (!_enable) return;
+ UnityEngine.PlayerPrefs.SetInt(key, value);
+ if (save)
+ {
+ UnityEngine.PlayerPrefs.Save();
+ }
+ }
+
+ ///
+ /// 设置字符串值(自动保存)。
+ ///
+ public static void SetString(string key, string value, bool save = true)
+ {
+ if (!_enable) return;
+ UnityEngine.PlayerPrefs.SetString(key, value);
+ if (save)
+ {
+ UnityEngine.PlayerPrefs.Save();
+ }
+ }
+
+ // 布尔值支持方法 -----------------------------
+
+ ///
+ /// 设置布尔值(存储为1或0)。
+ ///
+ public static void SetBool(string key, bool value)
+ {
+ if (!_enable) return;
+ SetInt(key, value ? 1 : 0);
+ }
+
+ ///
+ /// 获取布尔值(将存储的1/0转换为bool)。
+ ///
+ public static bool GetBool(string key, bool defaultValue)
+ {
+ if (!_enable) return defaultValue;
+ int defaultValue1 = defaultValue ? 1 : 0;
+ return GetInt(key, defaultValue1) == 1;
+ }
+
+ // 用户隔离支持方法 ---------------------------
+
+ ///
+ /// 生成用户隔离的复合键(格式:userId_key)。
+ ///
+ private static string GetUserKey(string key)
+ {
+ return $"{_userId}_{key}";
+ }
+
+ ///
+ /// 设置当前用户ID(用于键隔离)。
+ ///
+ /// 用户唯一标识。
+ public static void SetUserId(string id)
+ {
+ if (!_enable) return;
+ _userId = id;
+ }
+
+ ///
+ /// 检查用户隔离键是否存在。
+ ///
+ public static bool HasUserKey(string key)
+ {
+ return _enable && HasKey(GetUserKey(key));
+ }
+
+ // 用户隔离存储方法 ---------------------------
+
+ ///
+ /// 设置用户隔离的整数值。
+ ///
+ public static void SetUserInt(string key, int value)
+ {
+ if (!_enable) return;
+ SetInt(GetUserKey(key), value);
+ }
+
+ ///
+ /// 获取用户隔离的整数值。
+ ///
+ public static int GetUserInt(string key, int defaultValue)
+ {
+ return !_enable ? defaultValue : GetInt(GetUserKey(key), defaultValue);
+ }
+
+ ///
+ /// 设置用户隔离的浮点数值。
+ ///
+ public static void SetUserFloat(string key, float value)
+ {
+ if (!_enable) return;
+ SetFloat(GetUserKey(key), value);
+ }
+
+ ///
+ /// 获取用户隔离的浮点数值。
+ ///
+ public static float GetUserFloat(string key, float defaultValue)
+ {
+ return !_enable ? defaultValue : GetFloat(GetUserKey(key), defaultValue);
+ }
+
+ ///
+ /// 设置用户隔离的布尔值。
+ ///
+ public static void SetUserBool(string key, bool value)
+ {
+ if (!_enable) return;
+ SetInt(GetUserKey(key), value ? 1 : 0);
+ }
+
+ ///
+ /// 获取用户隔离的布尔值。
+ ///
+ public static bool GetUserBool(string key, bool defaultValue)
+ {
+ if (!_enable) return defaultValue;
+ int defaultValue1 = defaultValue ? 1 : 0;
+ return GetInt(GetUserKey(key), defaultValue1) == 1;
+ }
+
+ ///
+ /// 设置用户隔离的字符串值。
+ ///
+ public static void SetUserString(string key, string value)
+ {
+ if (!_enable) return;
+ SetString(GetUserKey(key), value);
+ }
+
+ ///
+ /// 获取用户隔离的字符串值。
+ ///
+ public static string GetUserString(string key, string defaultValue)
+ {
+ return !_enable ? defaultValue : GetString(GetUserKey(key), defaultValue);
+ }
+ }
+ }
+}
diff --git a/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.PlayerPrefs.cs.meta b/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.PlayerPrefs.cs.meta
new file mode 100644
index 00000000..6c34b192
--- /dev/null
+++ b/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.PlayerPrefs.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 0d554de3e8b4487db0f307f303bfc365
+timeCreated: 1741413315
\ No newline at end of file