|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using Unity.Serialization.Json; |
| 5 | +using UnityEngine; |
| 6 | + |
| 7 | +namespace Paps.UnityToolbarExtenderUIToolkit |
| 8 | +{ |
| 9 | + public static class UserSettingsPrefs |
| 10 | + { |
| 11 | + private static readonly string DIRECTORY = Path.Combine(Directory.GetParent(Application.dataPath).FullName, "UserSettings/", "unity-toolbar-extender-ui-toolkit", "user-settings-prefs"); |
| 12 | + private static readonly string FILE = Path.Combine(DIRECTORY, "user-settings-prefs.json"); |
| 13 | + |
| 14 | + private static Dictionary<string, object> _prefs; |
| 15 | + private static Dictionary<string, object> Prefs |
| 16 | + { |
| 17 | + get |
| 18 | + { |
| 19 | + if (_prefs == null) |
| 20 | + _prefs = Load(); |
| 21 | + |
| 22 | + return _prefs; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public static void SetInt(string key, int value) |
| 27 | + { |
| 28 | + Prefs[key] = value; |
| 29 | + Save(); |
| 30 | + } |
| 31 | + |
| 32 | + public static void SetFloat(string key, float value) |
| 33 | + { |
| 34 | + Prefs[key] = value; |
| 35 | + Save(); |
| 36 | + } |
| 37 | + |
| 38 | + public static void SetDouble(string key, double value) |
| 39 | + { |
| 40 | + Prefs[key] = value; |
| 41 | + Save(); |
| 42 | + } |
| 43 | + |
| 44 | + public static void SetBool(string key, bool value) |
| 45 | + { |
| 46 | + Prefs[key] = value; |
| 47 | + Save(); |
| 48 | + } |
| 49 | + |
| 50 | + public static void SetString(string key, string value) |
| 51 | + { |
| 52 | + Prefs[key] = value; |
| 53 | + Save(); |
| 54 | + } |
| 55 | + |
| 56 | + public static string GetString(string key, string defaultValue = null) |
| 57 | + { |
| 58 | + if (Prefs.ContainsKey(key)) |
| 59 | + return (string)Prefs[key]; |
| 60 | + |
| 61 | + return defaultValue; |
| 62 | + } |
| 63 | + |
| 64 | + public static bool GetBool(string key, bool defaultValue = false) |
| 65 | + { |
| 66 | + if (Prefs.ContainsKey(key)) |
| 67 | + return (bool)Prefs[key]; |
| 68 | + |
| 69 | + return defaultValue; |
| 70 | + } |
| 71 | + |
| 72 | + public static int GetInt(string key, int defaultValue = 0) |
| 73 | + { |
| 74 | + if (Prefs.ContainsKey(key)) |
| 75 | + return Convert.ToInt32(Prefs[key]); |
| 76 | + |
| 77 | + return defaultValue; |
| 78 | + } |
| 79 | + |
| 80 | + public static float GetFloat(string key, float defaultValue = 0.0f) |
| 81 | + { |
| 82 | + if (Prefs.ContainsKey(key)) |
| 83 | + return Convert.ToSingle(Prefs[key]); |
| 84 | + |
| 85 | + return defaultValue; |
| 86 | + } |
| 87 | + |
| 88 | + public static double GetDouble(string key, double defaultValue = 0.0) |
| 89 | + { |
| 90 | + if (Prefs.ContainsKey(key)) |
| 91 | + return Convert.ToDouble(Prefs[key]); |
| 92 | + |
| 93 | + return defaultValue; |
| 94 | + } |
| 95 | + |
| 96 | + private static Dictionary<string, object> Load() |
| 97 | + { |
| 98 | + if (!Directory.Exists(DIRECTORY)) |
| 99 | + Directory.CreateDirectory(DIRECTORY); |
| 100 | + |
| 101 | + if (!File.Exists(FILE)) |
| 102 | + return JsonSerialization.FromJson<Dictionary<string, object>>("{}"); |
| 103 | + |
| 104 | + var json = File.ReadAllText(FILE); |
| 105 | + |
| 106 | + var serializedDictionary = JsonSerialization.FromJson<Dictionary<string, object>>(json); |
| 107 | + |
| 108 | + return serializedDictionary; |
| 109 | + } |
| 110 | + |
| 111 | + private static void Save() |
| 112 | + { |
| 113 | + var json = JsonSerialization.ToJson(Prefs); |
| 114 | + |
| 115 | + File.WriteAllText(FILE, json); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments