TriLibSettings.cs 4.3 KB

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