File size: 4,803 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | namespace UnityEditor.SettingsManagement
{
/// <summary>
/// A settings repository backed by the UnityEditor.EditorPrefs class.
/// </summary>
public class UserSettingsRepository : ISettingsRepository
{
static string GetEditorPrefKey<T>(string key)
{
return GetEditorPrefKey(typeof(T).FullName, key);
}
static string GetEditorPrefKey(string fullName, string key)
{
return fullName + "::" + key;
}
static void SetEditorPref<T>(string key, T value)
{
var k = GetEditorPrefKey<T>(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<T>.Serialize(value));
}
static T GetEditorPref<T>(string key, T fallback = default(T))
{
var k = GetEditorPrefKey<T>(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<T>.Deserialize(EditorPrefs.GetString(k));
return (T)o;
}
/// <value>
/// What SettingsScope this repository applies to.
/// </value>
/// <inheritdoc cref="ISettingsRepository.scope"/>
public SettingsScope scope
{
get { return SettingsScope.User; }
}
/// <summary>
/// An identifying name for this repository. User settings are named "EditorPrefs."
/// </summary>
public string name
{
get { return "EditorPrefs"; }
}
/// <value>
/// File path to the serialized settings data.
/// </value>
/// <remarks>
/// This property returns an empty string.
/// </remarks>
/// <inheritdoc cref="ISettingsRepository.path"/>
public string path
{
get { return string.Empty; }
}
/// <summary>
/// Save all settings to their serialized state.
/// </summary>
/// <inheritdoc cref="ISettingsRepository.Save"/>
public void Save()
{
}
/// <inheritdoc />
/// <summary>
/// Set a value for key of type T.
/// </summary>
/// <param name="key">The settings key.</param>
/// <param name="value">The value to set. Must be serializable.</param>
/// <typeparam name="T">Type of value.</typeparam>
public void Set<T>(string key, T value)
{
SetEditorPref<T>(key, value);
}
/// <inheritdoc />
/// <summary>
/// Get a value with key of type T, or return the fallback value if no matching key is found.
/// </summary>
/// <param name="key">The settings key.</param>
/// <param name="fallback">If no key with a value of type T is found, this value is returned.</param>
/// <typeparam name="T">Type of value to search for.</typeparam>
public T Get<T>(string key, T fallback = default(T))
{
return GetEditorPref<T>(key, fallback);
}
/// <inheritdoc />
/// <summary>
/// Does the repository contain a setting with key and type.
/// </summary>
/// <param name="key">The settings key.</param>
/// <typeparam name="T">The type of value to search for.</typeparam>
/// <returns>True if a setting matching both key and type is found, false if no entry is found.</returns>
public bool ContainsKey<T>(string key)
{
return EditorPrefs.HasKey(GetEditorPrefKey<T>(key));
}
/// <inheritdoc />
/// <summary>
/// Remove a key value pair from the settings repository.
/// </summary>
/// <param name="key"></param>
/// <typeparam name="T"></typeparam>
public void Remove<T>(string key)
{
EditorPrefs.DeleteKey(GetEditorPrefKey<T>(key));
}
}
}
|