Tanoda
TriLibSettings.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using TriLibCore.Utils;
4#if UNITY_EDITOR
5using UnityEditor;
6#endif
7using UnityEngine;
8
9namespace TriLibCore
10{
15 public class TriLibSettings : ScriptableObject, ISerializationCallbackReceiver
16 {
17 private Dictionary<string, bool> _boolPreferences;
18 [SerializeField]
19 [HideInInspector]
20 private List<string> _boolKeys;
21 [SerializeField]
22 [HideInInspector]
23 private List<bool> _boolValues;
24
25 private static TriLibSettings GetTriLibPreferences()
26 {
27 var preferencesFiles = Resources.LoadAll<TriLibSettings>(string.Empty);
28 TriLibSettings triLibSettings;
29 if (preferencesFiles.Length == 0)
30 {
31#if UNITY_EDITOR
32 var triLibDirectories = AssetDatabase.FindAssets("TriLibMainFolderPlaceholder");
33 var triLibDirectory = triLibDirectories.Length > 0 ? FileUtils.GetFileDirectory(AssetDatabase.GUIDToAssetPath(triLibDirectories[0])) : "";
34 triLibSettings = CreateInstance<TriLibSettings>();
35 AssetDatabase.CreateAsset(triLibSettings, $"{triLibDirectory}/TriLibSettings.asset");
36 AssetDatabase.SaveAssets();
37#else
38 throw new Exception("Could not find TriLib preferences file.");
39#endif
40 }
41 else
42 {
43 if (preferencesFiles.Length > 1)
44 {
45 Debug.LogWarning("There is more than one TriLibSettings asset, and there is only one allowed per project.");
46 }
47 triLibSettings = preferencesFiles[0];
48 }
49 return triLibSettings;
50 }
51
52 public Dictionary<string, bool>.Enumerator GetKvp()
53 {
54 return _boolPreferences.GetEnumerator();
55 }
56
57 public static bool GetBool(string key)
58 {
59 var triLibPreferences = GetTriLibPreferences();
60 if (triLibPreferences._boolPreferences == null || !triLibPreferences._boolPreferences.TryGetValue(key, out var value))
61 {
62 return false;
63 }
64 return value;
65 }
66
67 public static void SetBool(string key, bool value)
68 {
69 var triLibPreferences = GetTriLibPreferences();
70 if (triLibPreferences._boolPreferences == null)
71 {
72 triLibPreferences._boolPreferences = new Dictionary<string, bool>();
73 }
74 triLibPreferences._boolPreferences[key] = value;
75#if UNITY_EDITOR
76 if (Application.isPlaying)
77 {
78 Debug.LogWarning("Can't save TriLib settings while in play mode. Please refer to the Project Settings/TriLib area.");
79 }
80 EditorUtility.SetDirty(triLibPreferences);
81 AssetDatabase.SaveAssets();
82 AssetDatabase.Refresh();
83#endif
84 }
85
86 public void OnBeforeSerialize()
87 {
88 if (_boolPreferences == null)
89 {
90 return;
91 }
92 if (_boolKeys == null || _boolValues == null)
93 {
94 _boolKeys = new List<string>();
95 _boolValues = new List<bool>();
96 }
97 _boolKeys.Clear();
98 _boolValues.Clear();
99 foreach (var kvp in _boolPreferences)
100 {
101 _boolKeys.Add(kvp.Key);
102 _boolValues.Add(kvp.Value);
103 }
104 }
105
106 public void OnAfterDeserialize()
107 {
108 if (_boolKeys == null || _boolValues == null)
109 {
110 return;
111 }
112 if (_boolPreferences == null)
113 {
114 _boolPreferences = new Dictionary<string, bool>();
115 }
116 _boolPreferences.Clear();
117 for (var i = 0; i < _boolKeys.Count; i++)
118 {
119 _boolPreferences.Add(_boolKeys[i], _boolValues[i]);
120 }
121 }
122 }
123}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
Represents the TriLib project settings provider. You can override this behavior to store the settings...
Dictionary< string, bool >.Enumerator GetKvp()
static void SetBool(string key, bool value)
static bool GetBool(string key)