Newer
Older
Simple-Multiplayer-Unity3D / Multiplayer Project / Library / PackageCache / com.unity.settings-manager@2.0.1 / Editor / UserSettingsRepository.cs
  1. namespace UnityEditor.SettingsManagement
  2. {
  3. /// <summary>
  4. /// Represents a settings repository for user preferences.
  5. /// </summary>
  6. /// <seealso cref="UnityEditor.EditorPrefs"/>
  7. public class UserSettingsRepository : ISettingsRepository
  8. {
  9. static string GetEditorPrefKey<T>(string key)
  10. {
  11. return GetEditorPrefKey(typeof(T).FullName, key);
  12. }
  13. static string GetEditorPrefKey(string fullName, string key)
  14. {
  15. return fullName + "::" + key;
  16. }
  17. static void SetEditorPref<T>(string key, T value)
  18. {
  19. var k = GetEditorPrefKey<T>(key);
  20. if (typeof(T) == typeof(string))
  21. EditorPrefs.SetString(k, (string)(object)value);
  22. else if (typeof(T) == typeof(bool))
  23. EditorPrefs.SetBool(k, (bool)(object)value);
  24. else if (typeof(T) == typeof(float))
  25. EditorPrefs.SetFloat(k, (float)(object)value);
  26. else if (typeof(T) == typeof(int))
  27. EditorPrefs.SetInt(k, (int)(object)value);
  28. else
  29. EditorPrefs.SetString(k, ValueWrapper<T>.Serialize(value));
  30. }
  31. static T GetEditorPref<T>(string key, T fallback = default(T))
  32. {
  33. var k = GetEditorPrefKey<T>(key);
  34. if (!EditorPrefs.HasKey(k))
  35. return fallback;
  36. var o = (object)fallback;
  37. if (typeof(T) == typeof(string))
  38. o = EditorPrefs.GetString(k, (string)o);
  39. else if (typeof(T) == typeof(bool))
  40. o = EditorPrefs.GetBool(k, (bool)o);
  41. else if (typeof(T) == typeof(float))
  42. o = EditorPrefs.GetFloat(k, (float)o);
  43. else if (typeof(T) == typeof(int))
  44. o = EditorPrefs.GetInt(k, (int)o);
  45. else
  46. return ValueWrapper<T>.Deserialize(EditorPrefs.GetString(k));
  47. return (T)o;
  48. }
  49. /// <summary>
  50. /// Gets the <see cref="UnityEditor.SettingsScope">scope</see> this repository applies to.
  51. /// </summary>
  52. /// <value>Indicates that this is a <see cref="UnityEditor.SettingsScope.User"/> preference.</value>
  53. /// <seealso cref="ISettingsRepository.scope"/>
  54. public SettingsScope scope
  55. {
  56. get { return SettingsScope.User; }
  57. }
  58. /// <summary>
  59. /// Gets the identifying name for this repository.
  60. /// </summary>
  61. /// <value>User settings are named "EditorPrefs".</value>
  62. public string name
  63. {
  64. get { return "EditorPrefs"; }
  65. }
  66. /// <summary>
  67. /// Gets the full path to the file containing the serialized settings data.
  68. /// </summary>
  69. /// <remarks>This property returns an empty string.</remarks>
  70. /// <value>The location stored for this repository.</value>
  71. /// <seealso cref="ISettingsRepository.path"/>
  72. public string path
  73. {
  74. get { return string.Empty; }
  75. }
  76. /// <summary>
  77. /// Saves all settings to their serialized state.
  78. /// </summary>
  79. /// <seealso cref="ISettingsRepository.Save"/>
  80. public void Save()
  81. {
  82. }
  83. /// <summary>
  84. /// Sets a value for a settings entry with a matching key and type `T`.
  85. /// </summary>
  86. /// <param name="key">The key used to identify the settings entry.</param>
  87. /// <param name="value">The value to set. This must be serializable.</param>
  88. /// <typeparam name="T">The type of value that this key points to.</typeparam>
  89. public void Set<T>(string key, T value)
  90. {
  91. SetEditorPref<T>(key, value);
  92. }
  93. /// <summary>
  94. /// Returns a value for a settings entry with a matching key and type `T`.
  95. /// </summary>
  96. /// <param name="key">The key used to identify the settings entry.</param>
  97. /// <param name="fallback">Specify the value of type `T` to return if the entry can't be found.</param>
  98. /// <typeparam name="T">The type of value that this key points to.</typeparam>
  99. /// <returns>The value matching both `key` and type `T`. If there was no match, this returns the `fallback` value.</returns>
  100. public T Get<T>(string key, T fallback = default(T))
  101. {
  102. return GetEditorPref<T>(key, fallback);
  103. }
  104. /// <summary>
  105. /// Determines whether this repository contains a settings entry that matches the specified key and is of type `T`.
  106. /// </summary>
  107. /// <param name="key">The key used to identify the settings entry.</param>
  108. /// <typeparam name="T">The type of value that this key points to.</typeparam>
  109. /// <returns>True if a settings entry matches both `key` and type `T`; false if no entry is found.</returns>
  110. public bool ContainsKey<T>(string key)
  111. {
  112. return EditorPrefs.HasKey(GetEditorPrefKey<T>(key));
  113. }
  114. /// <summary>
  115. /// Removes a key-value pair from this settings repository. This method identifies the settings entry to remove
  116. /// by matching the specified key for a value of type `T`.
  117. /// </summary>
  118. /// <param name="key">The key used to identify the settings entry.</param>
  119. /// <typeparam name="T">The type of value that this key points to.</typeparam>
  120. public void Remove<T>(string key)
  121. {
  122. EditorPrefs.DeleteKey(GetEditorPrefKey<T>(key));
  123. }
  124. }
  125. }