namespace UnityEditor.SettingsManagement { /// /// Represents a settings repository for user preferences. /// /// public class UserSettingsRepository : ISettingsRepository { static string GetEditorPrefKey(string key) { return GetEditorPrefKey(typeof(T).FullName, key); } static string GetEditorPrefKey(string fullName, string key) { return fullName + "::" + key; } static void SetEditorPref(string key, T value) { var k = GetEditorPrefKey(key); if (typeof(T) == typeof(string)) EditorPrefs.SetString(k, (string)(object)value); else if (typeof(T) == typeof(bool)) EditorPrefs.SetBool(k, (bool)(object)value); else if (typeof(T) == typeof(float)) EditorPrefs.SetFloat(k, (float)(object)value); else if (typeof(T) == typeof(int)) EditorPrefs.SetInt(k, (int)(object)value); else EditorPrefs.SetString(k, ValueWrapper.Serialize(value)); } static T GetEditorPref(string key, T fallback = default(T)) { var k = GetEditorPrefKey(key); if (!EditorPrefs.HasKey(k)) return fallback; var o = (object)fallback; if (typeof(T) == typeof(string)) o = EditorPrefs.GetString(k, (string)o); else if (typeof(T) == typeof(bool)) o = EditorPrefs.GetBool(k, (bool)o); else if (typeof(T) == typeof(float)) o = EditorPrefs.GetFloat(k, (float)o); else if (typeof(T) == typeof(int)) o = EditorPrefs.GetInt(k, (int)o); else return ValueWrapper.Deserialize(EditorPrefs.GetString(k)); return (T)o; } /// /// Gets the scope this repository applies to. /// /// Indicates that this is a preference. /// public SettingsScope scope { get { return SettingsScope.User; } } /// /// Gets the identifying name for this repository. /// /// User settings are named "EditorPrefs". public string name { get { return "EditorPrefs"; } } /// /// Gets the full path to the file containing the serialized settings data. /// /// This property returns an empty string. /// The location stored for this repository. /// public string path { get { return string.Empty; } } /// /// Saves all settings to their serialized state. /// /// public void Save() { } /// /// Sets a value for a settings entry with a matching key and type `T`. /// /// The key used to identify the settings entry. /// The value to set. This must be serializable. /// The type of value that this key points to. public void Set(string key, T value) { SetEditorPref(key, value); } /// /// Returns a value for a settings entry with a matching key and type `T`. /// /// The key used to identify the settings entry. /// Specify the value of type `T` to return if the entry can't be found. /// The type of value that this key points to. /// The value matching both `key` and type `T`. If there was no match, this returns the `fallback` value. public T Get(string key, T fallback = default(T)) { return GetEditorPref(key, fallback); } /// /// Determines whether this repository contains a settings entry that matches the specified key and is of type `T`. /// /// The key used to identify the settings entry. /// The type of value that this key points to. /// True if a settings entry matches both `key` and type `T`; false if no entry is found. public bool ContainsKey(string key) { return EditorPrefs.HasKey(GetEditorPrefKey(key)); } /// /// Removes a key-value pair from this settings repository. This method identifies the settings entry to remove /// by matching the specified key for a value of type `T`. /// /// The key used to identify the settings entry. /// The type of value that this key points to. public void Remove(string key) { EditorPrefs.DeleteKey(GetEditorPrefKey(key)); } } }