【m】框架优化
This commit is contained in:
279
Assets/00.StaryEvo/Runtime/Tool/CustomPlayerPrefs.cs
Normal file
279
Assets/00.StaryEvo/Runtime/Tool/CustomPlayerPrefs.cs
Normal file
@@ -0,0 +1,279 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
/// <summary>
|
||||
/// 自定义EditorPrefs实现,数据存储在项目根目录的EditorPrefs.ini文件中
|
||||
/// </summary>
|
||||
public static class CustomPlayerPrefs
|
||||
{
|
||||
private const string FileName = "PlayerPrefs.ini";
|
||||
private static string FilePath => Path.Combine(Application.persistentDataPath, FileName);
|
||||
|
||||
// 缓存的数据
|
||||
private static Dictionary<string, object> _dataCache;
|
||||
private static bool _isDataLoaded;
|
||||
|
||||
/// <summary>
|
||||
/// 设置字符串值
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
public static void SetString(string key, string value)
|
||||
{
|
||||
LoadData();
|
||||
_dataCache[key] = value;
|
||||
SaveData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取字符串值
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="defaultValue">默认值</param>
|
||||
/// <returns>值</returns>
|
||||
public static string GetString(string key, string defaultValue = "")
|
||||
{
|
||||
LoadData();
|
||||
_dataCache.TryGetValue(key, out var value);
|
||||
return value is string ? (string)value : defaultValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置整数值
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
public static void SetInt(string key, int value)
|
||||
{
|
||||
LoadData();
|
||||
_dataCache[key] = value;
|
||||
SaveData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取整数值
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="defaultValue">默认值</param>
|
||||
/// <returns>值</returns>
|
||||
public static int GetInt(string key, int defaultValue = 0)
|
||||
{
|
||||
LoadData();
|
||||
return _dataCache.TryGetValue(key, out var value) ? (int)value : defaultValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置浮点数值
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
public static void SetFloat(string key, float value)
|
||||
{
|
||||
LoadData();
|
||||
_dataCache[key] = value;
|
||||
SaveData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取浮点数值
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="defaultValue">默认值</param>
|
||||
/// <returns>值</returns>
|
||||
public static float GetFloat(string key, float defaultValue = 0.0f)
|
||||
{
|
||||
LoadData();
|
||||
return _dataCache.TryGetValue(key, out var value) ? (float)value : defaultValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置布尔值
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
public static void SetBool(string key, bool value)
|
||||
{
|
||||
LoadData();
|
||||
_dataCache[key] = value;
|
||||
SaveData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取布尔值
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="defaultValue">默认值</param>
|
||||
/// <returns>值</returns>
|
||||
public static bool GetBool(string key, bool defaultValue = false)
|
||||
{
|
||||
LoadData();
|
||||
return _dataCache.TryGetValue(key, out var value) ? (bool)value : defaultValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否存在指定的键
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <returns>是否存在</returns>
|
||||
public static bool HasKey(string key)
|
||||
{
|
||||
LoadData();
|
||||
return _dataCache.ContainsKey(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除指定的键
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
public static void DeleteKey(string key)
|
||||
{
|
||||
LoadData();
|
||||
if (_dataCache.Remove(key))
|
||||
{
|
||||
SaveData();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除所有数据
|
||||
/// </summary>
|
||||
public static void DeleteAll()
|
||||
{
|
||||
_dataCache.Clear();
|
||||
SaveData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有键
|
||||
/// </summary>
|
||||
/// <returns>键列表</returns>
|
||||
public static string[] GetAllKeys()
|
||||
{
|
||||
LoadData();
|
||||
return _dataCache.Keys.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有键值对
|
||||
/// </summary>
|
||||
/// <returns>键值对字典</returns>
|
||||
public static Dictionary<string, object> GetAll()
|
||||
{
|
||||
LoadData();
|
||||
return new Dictionary<string, object>(_dataCache);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从文件加载数据
|
||||
/// </summary>
|
||||
private static void LoadData()
|
||||
{
|
||||
if (_isDataLoaded && _dataCache != null)
|
||||
return;
|
||||
|
||||
_dataCache = new Dictionary<string, object>();
|
||||
|
||||
if (!File.Exists(FilePath))
|
||||
{
|
||||
_isDataLoaded = true;
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var lines = File.ReadAllLines(FilePath);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#"))
|
||||
continue;
|
||||
|
||||
var parts = line.Split(new[] { '=' }, 2);
|
||||
if (parts.Length != 2)
|
||||
continue;
|
||||
|
||||
var key = parts[0].Trim();
|
||||
var valueStr = parts[1].Trim();
|
||||
|
||||
// 尝试解析不同类型的值
|
||||
if (bool.TryParse(valueStr, out var boolValue))
|
||||
{
|
||||
_dataCache[key] = boolValue;
|
||||
}
|
||||
else if (int.TryParse(valueStr, out var intValue))
|
||||
{
|
||||
_dataCache[key] = intValue;
|
||||
}
|
||||
else if (float.TryParse(valueStr, out var floatValue))
|
||||
{
|
||||
_dataCache[key] = floatValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 字符串类型
|
||||
_dataCache[key] = valueStr;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"加载CustomPlayerPrefs数据失败: {e.Message}");
|
||||
_dataCache = new Dictionary<string, object>();
|
||||
}
|
||||
|
||||
_isDataLoaded = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存数据到文件
|
||||
/// </summary>
|
||||
private static void SaveData()
|
||||
{
|
||||
if (_dataCache == null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var lines = new List<string>
|
||||
{
|
||||
"# Custom Player Prefs Data",
|
||||
$"# Last Modified: {DateTime.Now}",
|
||||
""
|
||||
};
|
||||
|
||||
foreach (var kvp in _dataCache)
|
||||
{
|
||||
var valueStr = kvp.Value.ToString();
|
||||
lines.Add($"{kvp.Key}={valueStr}");
|
||||
}
|
||||
|
||||
// 确保目录存在
|
||||
var directory = Path.GetDirectoryName(FilePath);
|
||||
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
File.WriteAllLines(FilePath, lines);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"保存CustomPlayerPrefs数据失败: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 手动刷新数据(重新从文件加载)
|
||||
/// </summary>
|
||||
public static void Refresh()
|
||||
{
|
||||
_isDataLoaded = false;
|
||||
LoadData();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ab4705a457b8462f8ec9023e35859260
|
||||
timeCreated: 1767771190
|
||||
@@ -21,7 +21,7 @@ namespace Stary.Evo
|
||||
#if UNITY_EDITOR
|
||||
authorization = EditorPrefs.GetString("Authorization");
|
||||
#else
|
||||
authorization = PlayerPrefs.GetString("Authorization");
|
||||
authorization = CustomPlayerPrefs.GetString("Authorization");
|
||||
#endif
|
||||
|
||||
}
|
||||
@@ -72,7 +72,7 @@ namespace Stary.Evo
|
||||
#if UNITY_EDITOR
|
||||
EditorPrefs.SetString("Authorization", authorization);
|
||||
#else
|
||||
PlayerPrefs.SetString("Authorization",authorization);
|
||||
CustomPlayerPrefs.SetString("Authorization",authorization);
|
||||
#endif
|
||||
|
||||
Debug.Log("UnityEvo:登录成功");
|
||||
@@ -100,7 +100,7 @@ namespace Stary.Evo
|
||||
#if UNITY_EDITOR
|
||||
authorization = EditorPrefs.GetString("Authorization");
|
||||
#else
|
||||
authorization = PlayerPrefs.GetString("Authorization");
|
||||
authorization = CustomPlayerPrefs.GetString("Authorization");
|
||||
#endif
|
||||
try
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user