StoredEditorValue.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEditor;
  6. namespace TheraBytes.BetterUi.Editor
  7. {
  8. // //////////
  9. // STRING //
  10. // ////////
  11. public class StoredEditorString : StoredEditorValue<string>
  12. {
  13. public StoredEditorString(string id, string defaultValue)
  14. : base(id, defaultValue)
  15. { }
  16. protected override string GetPrefValue(string id)
  17. {
  18. return EditorPrefs.GetString(id);
  19. }
  20. protected override void SavePrefValue(string id, string value)
  21. {
  22. EditorPrefs.SetString(id, value);
  23. }
  24. }
  25. // /////////
  26. // FLOAT //
  27. // ///////
  28. public class StoredEditorFloat : StoredEditorValue<float>
  29. {
  30. public StoredEditorFloat(string id, float defaultValue)
  31. : base(id, defaultValue)
  32. { }
  33. protected override float GetPrefValue(string id)
  34. {
  35. return EditorPrefs.GetInt(id);
  36. }
  37. protected override void SavePrefValue(string id, float value)
  38. {
  39. EditorPrefs.SetFloat(id, value);
  40. }
  41. }
  42. // ///////
  43. // INT //
  44. // /////
  45. public class StoredEditorInt : StoredEditorValue<int>
  46. {
  47. public StoredEditorInt(string id, int defaultValue)
  48. : base(id, defaultValue)
  49. { }
  50. protected override int GetPrefValue(string id)
  51. {
  52. return EditorPrefs.GetInt(id);
  53. }
  54. protected override void SavePrefValue(string id, int value)
  55. {
  56. EditorPrefs.SetInt(id, value);
  57. }
  58. }
  59. // ////////
  60. // BOOL //
  61. // //////
  62. public class StoredEditorBool : StoredEditorValue<bool>
  63. {
  64. public StoredEditorBool(string id, bool defaultValue)
  65. : base(id, defaultValue)
  66. { }
  67. protected override bool GetPrefValue(string id)
  68. {
  69. return EditorPrefs.GetBool(id);
  70. }
  71. protected override void SavePrefValue(string id, bool value)
  72. {
  73. EditorPrefs.SetBool(id, value);
  74. }
  75. }
  76. // //////////////////////
  77. // GENERIC BASE CLASS //
  78. // ////////////////////
  79. public abstract class StoredEditorValue<T>
  80. {
  81. public T Value
  82. {
  83. get
  84. {
  85. if (EditorPrefs.HasKey(id))
  86. return GetPrefValue(id);
  87. return defaultValue;
  88. }
  89. set
  90. {
  91. SavePrefValue(id, value);
  92. }
  93. }
  94. T defaultValue;
  95. string id;
  96. protected StoredEditorValue(string id, T defaultValue)
  97. {
  98. this.id = id;
  99. this.defaultValue = defaultValue;
  100. }
  101. protected abstract void SavePrefValue(string id, T value);
  102. protected abstract T GetPrefValue(string id);
  103. public static implicit operator T(StoredEditorValue<T> self)
  104. {
  105. return self.Value;
  106. }
  107. }
  108. }