BoolPref.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. #if UNITY_EDITOR
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace Animancer.Editor
  6. {
  7. /// <summary>[Editor-Only]
  8. /// A simple wrapper around <see cref="EditorPrefs"/> to get and set a bool.
  9. /// <para></para>
  10. /// If you are interested in a more comprehensive pref wrapper that supports more types, you should check out
  11. /// <see href="https://kybernetik.com.au/inspector-gadgets/docs/other/auto-prefs">Inspector Gadgets - Auto Prefs</see>.
  12. /// </summary>
  13. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/BoolPref
  14. ///
  15. public class BoolPref
  16. {
  17. /************************************************************************************************************************/
  18. /// <summary>The prefix which is automatically added before the <see cref="Key"/>.</summary>
  19. public const string KeyPrefix = nameof(Animancer) + "/";
  20. /// <summary>The identifier with which this pref will be saved.</summary>
  21. public readonly string Key;
  22. /// <summary>The label to use when adding a function to toggle this pref to a menu.</summary>
  23. public readonly string MenuItem;
  24. /// <summary>The starting value to use for this pref if none was previously saved.</summary>
  25. public readonly bool DefaultValue;
  26. /************************************************************************************************************************/
  27. private bool _HasValue;
  28. private bool _Value;
  29. /// <summary>The current value of this pref.</summary>
  30. public bool Value
  31. {
  32. get
  33. {
  34. if (!_HasValue)
  35. {
  36. _HasValue = true;
  37. _Value = EditorPrefs.GetBool(Key, DefaultValue);
  38. }
  39. return _Value;
  40. }
  41. set
  42. {
  43. if (_Value == value &&
  44. _HasValue)
  45. return;
  46. _Value = value;
  47. _HasValue = true;
  48. EditorPrefs.SetBool(Key, value);
  49. }
  50. }
  51. /// <summary>Returns the current value of the `pref`.</summary>
  52. public static implicit operator bool(BoolPref pref) => pref.Value;
  53. /************************************************************************************************************************/
  54. /// <summary>Creates a new <see cref="BoolPref"/>.</summary>
  55. public BoolPref(string menuItem, bool defaultValue)
  56. : this(null, menuItem, defaultValue) { }
  57. /// <summary>Creates a new <see cref="BoolPref"/>.</summary>
  58. public BoolPref(string keyPrefix, string menuItem, bool defaultValue)
  59. {
  60. MenuItem = menuItem + " ?";
  61. Key = KeyPrefix + keyPrefix + menuItem;
  62. DefaultValue = defaultValue;
  63. }
  64. /************************************************************************************************************************/
  65. /// <summary>Adds a menu function to toggle the <see cref="Value"/> of this pref.</summary>
  66. public void AddToggleFunction(GenericMenu menu)
  67. {
  68. menu.AddItem(new GUIContent(MenuItem), Value, () =>
  69. {
  70. Value = !Value;
  71. });
  72. }
  73. /************************************************************************************************************************/
  74. /// <summary>Returns a string containing the <see cref="Key"/> and <see cref="Value"/>.</summary>
  75. public override string ToString() => $"{nameof(BoolPref)} ({nameof(Key)} = '{Key}', {nameof(Value)} = {Value})";
  76. /************************************************************************************************************************/
  77. }
  78. }
  79. #endif