static void GetEditorPrefsValue <T>(string prefsKey, out T prefValue) { if (TryGetCachedEditorPrefsValue(prefsKey, out prefValue)) { return; } var type = typeof(T); var prefsSet = false; if (type == typeof(bool)) { prefValue = (T)(object)EditorPrefs.GetBool(prefsKey); prefsSet = true; } else if (type == typeof(int)) { prefValue = (T)(object)EditorPrefs.GetInt(prefsKey); prefsSet = true; } else if (type == typeof(float)) { prefValue = (T)(object)EditorPrefs.GetFloat(prefsKey); prefsSet = true; } else if (type == typeof(string)) { prefValue = (T)(object)EditorPrefs.GetString(prefsKey); prefsSet = true; } else if (type.IsAssignableFromOrSubclassOf(typeof(Enum))) { prefValue = (T)(object)EditorPrefs.GetInt(prefsKey); prefsSet = true; } else if (type == typeof(Color)) { prefValue = (T)(object)EditorMaterialUtils.PrefToColor(EditorPrefs.GetString(prefsKey)); prefsSet = true; } else { Debug.LogError(string.Format("Could not get Editor Preference Default of type : {0} Type is not supported!", type)); } if (prefsSet && prefValue != null) { SetEditorPrefsValue(prefsKey, prefValue); return; } SetEditorPrefsValue(prefsKey, default(T)); prefValue = default(T); }
static void SetEditorPrefsValue <T>(string prefsKey, T value) { T cachedValue; if (TryGetCachedEditorPrefsValue(prefsKey, out cachedValue) && cachedValue.Equals(value)) { return; } var type = typeof(T); if (type == typeof(bool)) { EditorPrefs.SetBool(prefsKey, (bool)(object)value); } else if (type == typeof(int) && value is int) { EditorPrefs.SetInt(prefsKey, (int)(object)value); } else if (type == typeof(float) && value is float) { EditorPrefs.SetFloat(prefsKey, (float)(object)value); } else if (type == typeof(string) && value is string) { EditorPrefs.SetString(prefsKey, (string)(object)value); } else if (type.IsAssignableFromOrSubclassOf(typeof(Enum)) && value.GetType().IsAssignableFromOrSubclassOf(typeof(Enum))) { EditorPrefs.SetInt(prefsKey, (int)(object)value); } else if (type == typeof(Color) && value is Color) { EditorPrefs.SetString(prefsKey, EditorMaterialUtils.ColorToColorPref(prefsKey, (Color)(object)value)); } else { Debug.LogError(string.Format("Could not set Editor Preference Value of type : {0} with value {1} !", type, value)); return; } if (k_EditorPrefsValueSessionCache.ContainsKey(prefsKey)) { k_EditorPrefsValueSessionCache[prefsKey] = value; } else { k_EditorPrefsValueSessionCache.Add(prefsKey, value); } }