201 lines
4.9 KiB
C#
201 lines
4.9 KiB
C#
|
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<string, string> _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);
|
|||
|
#endif
|
|||
|
if (!File.Exists(filePath))
|
|||
|
{
|
|||
|
File.Create(filePath).Close();
|
|||
|
_dictionary = new Dictionary<string, string>();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
string json = File.ReadAllText(filePath);
|
|||
|
if (!string.IsNullOrEmpty(json))
|
|||
|
{
|
|||
|
_dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_dictionary = new Dictionary<string, string>();
|
|||
|
}
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
_dictionary = new Dictionary<string, string>();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
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)
|
|||
|
{
|
|||
|
if (!_dictionary.TryGetValue(key, out var value1))
|
|||
|
{
|
|||
|
throw new Exception($"Could not has int {key}");
|
|||
|
}
|
|||
|
|
|||
|
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)
|
|||
|
{
|
|||
|
if (!_dictionary.TryGetValue(key, out var value1))
|
|||
|
{
|
|||
|
throw new Exception($"Could not has float {key}");
|
|||
|
}
|
|||
|
|
|||
|
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)
|
|||
|
{
|
|||
|
if (!_dictionary.TryGetValue(key, out var value1))
|
|||
|
{
|
|||
|
throw new Exception($"Could not has {key}");
|
|||
|
}
|
|||
|
|
|||
|
return value1;
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Bool
|
|||
|
|
|||
|
public static void SetBool(string key, bool value)
|
|||
|
{
|
|||
|
_dictionary[key] = value.ToString();
|
|||
|
SaveData();
|
|||
|
}
|
|||
|
|
|||
|
public static bool GetBool(string key)
|
|||
|
{
|
|||
|
if (!_dictionary.TryGetValue(key, out var value1))
|
|||
|
{
|
|||
|
throw new Exception($"Could not has bool {key}");
|
|||
|
}
|
|||
|
|
|||
|
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)
|
|||
|
{
|
|||
|
if (!_dictionary.TryGetValue(key, out var value1))
|
|||
|
{
|
|||
|
throw new Exception($"Could not has color {key}");
|
|||
|
}
|
|||
|
|
|||
|
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
|
|||
|
}
|
|||
|
}
|