using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
namespace Stary.Evo
{
///
/// 自定义PlayerPrefs实现,数据存储在项目根目录的PlayerPrefs.ini文件中
///
public static class CustomPlayerPrefs
{
private const string FileName = "PlayerPrefs.ini";
private static string FilePath => Path.Combine(Application.persistentDataPath, FileName);
// 缓存的数据
private static Dictionary _dataCache;
private static bool _isDataLoaded;
///
/// 设置字符串值
///
/// 键
/// 值
public static void SetString(string key, string value)
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
PlayerPrefs.SetString(key, value);
PlayerPrefs.Save();
}
else
{
LoadData();
_dataCache[key] = value;
SaveData();
}
}
///
/// 获取字符串值
///
/// 键
/// 默认值
/// 值
public static string GetString(string key, string 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;
}
}
///
/// 设置整数值
///
/// 键
/// 值
public static void SetInt(string key, int value)
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
PlayerPrefs.SetInt(key, value);
PlayerPrefs.Save();
}
else
{
LoadData();
_dataCache[key] = value;
SaveData();
}
}
///
/// 获取整数值
///
/// 键
/// 默认值
/// 值
public static int GetInt(string key, int defaultValue = 0)
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
return PlayerPrefs.GetInt(key, defaultValue);
}
else
{
LoadData();
return _dataCache.TryGetValue(key, out var value) ? (int)value : defaultValue;
}
}
///
/// 设置浮点数值
///
/// 键
/// 值
public static void SetFloat(string key, float value)
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
PlayerPrefs.SetFloat(key, value);
PlayerPrefs.Save();
}
else
{
LoadData();
_dataCache[key] = value;
SaveData();
}
}
///
/// 获取浮点数值
///
/// 键
/// 默认值
/// 值
public static float GetFloat(string key, float defaultValue = 0.0f)
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
return PlayerPrefs.GetFloat(key, defaultValue);
}
else
{
LoadData();
return _dataCache.TryGetValue(key, out var value) ? (float)value : defaultValue;
}
}
///
/// 设置布尔值
///
/// 键
/// 值
public static void SetBool(string key, bool value)
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
PlayerPrefs.SetInt(key, value ? 1 : 0);
PlayerPrefs.Save();
}
else
{
LoadData();
_dataCache[key] = value;
SaveData();
}
}
///
/// 获取布尔值
///
/// 键
/// 默认值
/// 值
public static bool GetBool(string key, bool defaultValue = false)
{
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;
}
}
///
/// 检查是否存在指定的键
///
/// 键
/// 是否存在
public static bool HasKey(string key)
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
return PlayerPrefs.HasKey(key);
}
else
{
LoadData();
return _dataCache.ContainsKey(key);
}
}
///
/// 删除指定的键
///
/// 键
public static void DeleteKey(string key)
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
PlayerPrefs.DeleteKey(key);
PlayerPrefs.Save();
}
else
{
LoadData();
if (_dataCache.Remove(key))
{
SaveData();
}
}
}
///
/// 清除所有数据
///
public static void DeleteAll()
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
PlayerPrefs.DeleteAll();
PlayerPrefs.Save();
}
else
{
_dataCache.Clear();
SaveData();
}
}
///
/// 获取所有键
///
/// 键列表
public static string[] GetAllKeys()
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
// WebGL平台无法直接获取所有键,这里返回空数组
// 注意:实际项目中可能需要维护一个键的列表
return new string[0];
}
else
{
LoadData();
return _dataCache.Keys.ToArray();
}
}
///
/// 获取所有键值对
///
/// 键值对字典
public static Dictionary GetAll()
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
// WebGL平台无法直接获取所有键值对,这里返回空字典
// 注意:实际项目中可能需要维护一个键值对的字典
return new Dictionary();
}
else
{
LoadData();
return new Dictionary(_dataCache);
}
}
///
/// 从文件加载数据
///
private static void LoadData()
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
// WebGL平台使用PlayerPrefs,不需要从文件加载
return;
}
if (_isDataLoaded && _dataCache != null)
return;
_dataCache = new Dictionary();
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();
}
_isDataLoaded = true;
}
///
/// 保存数据到文件
///
private static void SaveData()
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
// WebGL平台使用PlayerPrefs,不需要保存到文件
return;
}
if (_dataCache == null)
return;
try
{
var lines = new List
{
"# 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}");
}
}
///
/// 手动刷新数据(重新从文件加载)
///
public static void Refresh()
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
// WebGL平台使用PlayerPrefs,不需要刷新
return;
}
_isDataLoaded = false;
LoadData();
}
}
}