【m】更新持久化存储
Some checks failed
Plugin Library CI / publish (00.ARMazTools) (push) Failing after 3s
Plugin Library CI / publish (00.BuildOriginality) (push) Successful in 4s
Plugin Library CI / publish (00.StaryEvo) (push) Successful in 6s
Plugin Library CI / publish (00.StaryEvoTools) (push) Successful in 19s
Plugin Library CI / publish (01.HybridCLR) (push) Successful in 6s
Plugin Library CI / publish (04.AudioCore) (push) Has been cancelled
Plugin Library CI / publish (05.TableTextConversion) (push) Has been cancelled
Plugin Library CI / publish (06.UIFarme) (push) Has been cancelled
Plugin Library CI / publish (07.RKTools) (push) Has been cancelled
Plugin Library CI / publish (08.UniTask) (push) Has been cancelled
Plugin Library CI / publish (09.CodeChecker) (push) Has been cancelled
Plugin Library CI / publish (10.StoryEditor) (push) Has been cancelled
Plugin Library CI / publish (10.XNode) (push) Has been cancelled
Plugin Library CI / publish (11.PointCloudTools) (push) Has been cancelled
Plugin Library CI / publish (03.YooAsset) (push) Has been cancelled
Plugin Library CI / publish (02.InformationSave) (push) Successful in 3s
Some checks failed
Plugin Library CI / publish (00.ARMazTools) (push) Failing after 3s
Plugin Library CI / publish (00.BuildOriginality) (push) Successful in 4s
Plugin Library CI / publish (00.StaryEvo) (push) Successful in 6s
Plugin Library CI / publish (00.StaryEvoTools) (push) Successful in 19s
Plugin Library CI / publish (01.HybridCLR) (push) Successful in 6s
Plugin Library CI / publish (04.AudioCore) (push) Has been cancelled
Plugin Library CI / publish (05.TableTextConversion) (push) Has been cancelled
Plugin Library CI / publish (06.UIFarme) (push) Has been cancelled
Plugin Library CI / publish (07.RKTools) (push) Has been cancelled
Plugin Library CI / publish (08.UniTask) (push) Has been cancelled
Plugin Library CI / publish (09.CodeChecker) (push) Has been cancelled
Plugin Library CI / publish (10.StoryEditor) (push) Has been cancelled
Plugin Library CI / publish (10.XNode) (push) Has been cancelled
Plugin Library CI / publish (11.PointCloudTools) (push) Has been cancelled
Plugin Library CI / publish (03.YooAsset) (push) Has been cancelled
Plugin Library CI / publish (02.InformationSave) (push) Successful in 3s
This commit is contained in:
277
Assets/00.StaryEvo/Runtime/Tool/CustomEditorPrefs.cs
Normal file
277
Assets/00.StaryEvo/Runtime/Tool/CustomEditorPrefs.cs
Normal file
@@ -0,0 +1,277 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
/// <summary>
|
||||
/// 自定义EditorPrefs实现,数据存储在项目根目录的EditorPrefs.ini文件中
|
||||
/// </summary>
|
||||
public static class CustomEditorPrefs
|
||||
{
|
||||
private const string FileName = "EditorPrefs.ini";
|
||||
private static string FilePath => Path.Combine(Application.dataPath, "..", 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();
|
||||
return _dataCache.TryGetValue(key, out var value) ? (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($"加载CustomEditorPrefs数据失败: {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 Editor 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($"保存CustomEditorPrefs数据失败: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 手动刷新数据(重新从文件加载)
|
||||
/// </summary>
|
||||
public static void Refresh()
|
||||
{
|
||||
_isDataLoaded = false;
|
||||
LoadData();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 794f2171aa6c41a3a0845f2dd0b9ef91
|
||||
timeCreated: 1767769690
|
||||
@@ -19,7 +19,7 @@ namespace Stary.Evo
|
||||
static WebRequestSystem()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
authorization = EditorPrefs.GetString("Authorization");
|
||||
authorization = CustomEditorPrefs.GetString("Authorization");
|
||||
#else
|
||||
authorization = CustomPlayerPrefs.GetString("Authorization");
|
||||
#endif
|
||||
@@ -70,7 +70,7 @@ namespace Stary.Evo
|
||||
Debug.Log("UnityEvo:AuthenticationResponse" + authResponseData.Token);
|
||||
authorization = authResponseData.Token;
|
||||
#if UNITY_EDITOR
|
||||
EditorPrefs.SetString("Authorization", authorization);
|
||||
CustomEditorPrefs.SetString("Authorization", authorization);
|
||||
#else
|
||||
CustomPlayerPrefs.SetString("Authorization",authorization);
|
||||
#endif
|
||||
@@ -98,7 +98,7 @@ namespace Stary.Evo
|
||||
public static async Task<bool> GetValidateToken(string url)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
authorization = EditorPrefs.GetString("Authorization");
|
||||
authorization = CustomEditorPrefs.GetString("Authorization");
|
||||
#else
|
||||
authorization = CustomPlayerPrefs.GetString("Authorization");
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user