EditorPreferences.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License. See LICENSE in the project root for license information.
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace XRTool.Util
  6. {
  7. /// <summary>
  8. /// Convenience class for setting Editor Preferences with <see href="https://docs.unity3d.com/ScriptReference/Application-productName.html">Application.productName</see> as key prefix.
  9. /// </summary>
  10. public static class EditorPreferences
  11. {
  12. /// <summary>
  13. /// Set the saved <see cref="string"/> from to <see href="https://docs.unity3d.com/ScriptReference/EditorPrefs.html">EditorPrefs</see>.
  14. /// </summary>
  15. public static void Set(string key, string value)
  16. {
  17. Debug.Assert(!string.IsNullOrWhiteSpace(key));
  18. EditorPrefs.SetString($"{Application.productName}_{key}", value);
  19. }
  20. /// <summary>
  21. /// Set the saved <see cref="bool"/> from to <see href="https://docs.unity3d.com/ScriptReference/EditorPrefs.html">EditorPrefs</see>.
  22. /// </summary>
  23. public static void Set(string key, bool value)
  24. {
  25. Debug.Assert(!string.IsNullOrWhiteSpace(key));
  26. EditorPrefs.SetBool($"{Application.productName}_{key}", value);
  27. }
  28. /// <summary>
  29. /// Set the saved <see cref="float"/> from the <see href="https://docs.unity3d.com/ScriptReference/EditorPrefs.html">EditorPrefs</see>.
  30. /// </summary>
  31. public static void Set(string key, float value)
  32. {
  33. Debug.Assert(!string.IsNullOrWhiteSpace(key));
  34. EditorPrefs.SetFloat($"{Application.productName}_{key}", value);
  35. }
  36. /// <summary>
  37. /// Set the saved <see cref="int"/> from the<see href="https://docs.unity3d.com/ScriptReference/EditorPrefs.html">EditorPrefs</see>.
  38. /// </summary>
  39. public static void Set(string key, int value)
  40. {
  41. Debug.Assert(!string.IsNullOrWhiteSpace(key));
  42. EditorPrefs.SetInt($"{Application.productName}_{key}", value);
  43. }
  44. /// <summary>
  45. /// Get the saved <see cref="string"/> from the<see href="https://docs.unity3d.com/ScriptReference/EditorPrefs.html">EditorPrefs</see>.
  46. /// </summary>
  47. public static string Get(string key, string defaultValue)
  48. {
  49. Debug.Assert(!string.IsNullOrWhiteSpace(key));
  50. if (EditorPrefs.HasKey($"{Application.productName}_{key}"))
  51. {
  52. return EditorPrefs.GetString($"{Application.productName}_{key}");
  53. }
  54. EditorPrefs.SetString($"{Application.productName}_{key}", defaultValue);
  55. return defaultValue;
  56. }
  57. /// <summary>
  58. /// Get the saved <see cref="bool"/> from the <see href="https://docs.unity3d.com/ScriptReference/EditorPrefs.html">EditorPrefs</see>.
  59. /// </summary>
  60. public static bool Get(string key, bool defaultValue)
  61. {
  62. Debug.Assert(!string.IsNullOrWhiteSpace(key));
  63. if (EditorPrefs.HasKey($"{Application.productName}_{key}"))
  64. {
  65. return EditorPrefs.GetBool($"{Application.productName}_{key}");
  66. }
  67. EditorPrefs.SetBool($"{Application.productName}_{key}", defaultValue);
  68. return defaultValue;
  69. }
  70. /// <summary>
  71. /// Get the saved <see cref="float"/> from the <see href="https://docs.unity3d.com/ScriptReference/EditorPrefs.html">EditorPrefs</see>.
  72. /// </summary>
  73. public static float Get(string key, float defaultValue)
  74. {
  75. Debug.Assert(!string.IsNullOrWhiteSpace(key));
  76. if (EditorPrefs.HasKey($"{Application.productName}_{key}"))
  77. {
  78. return EditorPrefs.GetFloat($"{Application.productName}_{key}");
  79. }
  80. EditorPrefs.SetFloat($"{Application.productName}_{key}", defaultValue);
  81. return defaultValue;
  82. }
  83. /// <summary>
  84. /// Get the saved <see cref="int"/> from the <see href="https://docs.unity3d.com/ScriptReference/EditorPrefs.html">EditorPrefs</see>.
  85. /// </summary>
  86. public static int Get(string key, int defaultValue)
  87. {
  88. Debug.Assert(!string.IsNullOrWhiteSpace(key));
  89. if (EditorPrefs.HasKey($"{Application.productName}_{key}"))
  90. {
  91. return EditorPrefs.GetInt($"{Application.productName}_{key}");
  92. }
  93. EditorPrefs.SetInt($"{Application.productName}_{key}", defaultValue);
  94. return defaultValue;
  95. }
  96. }
  97. }