using System; using System.Collections.Generic; using System.Globalization; using System.IO; using Newtonsoft.Json; using UnityEngine; namespace HK { public static class PlayerPersistent { private static string filePath; private static Dictionary _dictionary; static PlayerPersistent() { string fileName = "PlayerPersistent.json"; #if PLATFORM_STANDALONE_WIN || UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN filePath = Path.Combine(Application.streamingAssetsPath, fileName); #elif UNITY_ANDROID filePath = Path.Combine(Application.persistentDataPath, fileName); #elif PLATFORM_WEBGL throw new NullReferenceException("webgl platform Cannot load player persistent file."); #endif if (!File.Exists(filePath)) { File.Create(filePath).Close(); _dictionary = new Dictionary(); } else { try { string json = File.ReadAllText(filePath); if (!string.IsNullOrEmpty(json)) { _dictionary = JsonConvert.DeserializeObject>(json); } else { _dictionary = new Dictionary(); } } catch { _dictionary = new Dictionary(); } } } static void SaveData() { string json = JsonConvert.SerializeObject(_dictionary, Formatting.Indented); File.WriteAllText(filePath, json); } #region Int public static void SetInt(string key, int value) { _dictionary[key] = value.ToString(); SaveData(); } public static int GetInt(string key) { return TryGetInt(key); } private static int TryGetInt(string key) { if (!_dictionary.TryGetValue(key, out var value1)) { Debug.Log($"Could not has int {key}"); return int.MinValue; } if (!int.TryParse(value1, out int value)) { throw new Exception("Could not parse int"); } return value; } #endregion #region Float public static void SetFloat(string key, float value) { _dictionary[key] = value.ToString(CultureInfo.InvariantCulture); SaveData(); } public static float GetFloat(string key) { return TryGetFloat(key); } private static float TryGetFloat(string key) { if (!_dictionary.TryGetValue(key, out var value1)) { Debug.Log($"Could not has float {key}"); return float.MinValue; } if (!float.TryParse(value1, out float value)) { throw new Exception("Could not parse float"); } return value; } #endregion #region String public static void SetString(string key, string value) { _dictionary[key] = value; SaveData(); } public static string GetString(string key) { return TryGetString(key); } private static string TryGetString(string key) { if (!_dictionary.TryGetValue(key, out var value1)) { Debug.Log($"Could not has {key}"); return string.Empty; } return value1; } #endregion #region Bool public static void SetBool(string key, bool value) { _dictionary[key] = value.ToString(); SaveData(); } public static bool GetBool(string key) { return TryGetBool(key); } private static bool TryGetBool(string key) { if (!_dictionary.TryGetValue(key, out var value1)) { Debug.Log($"Could not has bool {key}"); return false; } if (!bool.TryParse(value1, out bool value)) { throw new Exception("Could not parse bool"); } return value; } #endregion #region Color public static void SetColor(string key, Color value) { _dictionary[key] = value.ToString(); SaveData(); } public static Color GetColor(string key) { return TryGetColor(key); } private static Color TryGetColor(string key) { if (!_dictionary.TryGetValue(key, out var value1)) { Debug.Log($"Could not has color {key}"); return Color.white; } if (!ColorUtility.TryParseHtmlString(value1, out Color value)) { throw new Exception($"Could not parse color {key}"); } return value; } #endregion #region Delete public static bool HasKey(string key) { return _dictionary.ContainsKey(key); } public static bool DeleteKey(string key) { if (_dictionary.Remove(key)) { SaveData(); return true; } return false; } public static void DeleteAll() { _dictionary.Clear(); SaveData(); } #endregion } }