using System; using UnityEditor; using UnityEditorInternal; using UnityEngine; namespace FullscreenEditor { /// Helper class for saving preferences. /// The type you want to save, must be marked as [Serializable] public sealed class PrefItem { [SerializeField] private T savedValue; /// The key for saving the value. public string Key { get; private set; } /// The default value to use when there's none saved. public T DefaultValue { get; private set; } /// A label and an explanation of what this item is for. public GUIContent Content { get; private set; } /// Callback called whenever the saved value changes. public Action OnValueSaved { get; set; } /// The value saved by this instance. public T Value { get { return savedValue; } set { if (!savedValue.Equals(value)) { savedValue = value; SaveValue(); } } } public PrefItem(string key, T defaultValue, string text, string tooltip) { Key = "Fullscreen." + key; FullscreenPreferences.onLoadDefaults += DeleteValue; Content = new GUIContent(text, tooltip); FullscreenPreferences.contents.Add(Content); DefaultValue = savedValue = defaultValue; LoadValue(); } private void LoadValue() { try { if (EditorPrefs.HasKey(Key)) JsonUtility.FromJsonOverwrite(EditorPrefs.GetString(Key), this); } catch (Exception e) { Logger.Warning("Failed to load {0}, using default value: {1}", Key, e); savedValue = DefaultValue; SaveValue(); } } public void SaveValue() { try { EditorPrefs.SetString(Key, JsonUtility.ToJson(this)); Logger.Debug("Saved value to key {0}:\n{1}", Key, EditorPrefs.GetString(Key)); if (OnValueSaved != null) OnValueSaved.Invoke(Value); InternalEditorUtility.RepaintAllViews(); } catch (Exception e) { Logger.Warning("Failed to save {0}: {1}", Key, e); } } public void DeleteValue() { EditorPrefs.DeleteKey(Key); savedValue = DefaultValue; if (OnValueSaved != null) OnValueSaved.Invoke(savedValue); } public static implicit operator T(PrefItem pb) { return pb.Value; } public static implicit operator GUIContent(PrefItem pb) { return pb.Content; } } /// Helper class for drawing the in the . public static class PrefItemGUI { public static void DoGUI(this PrefItem pref) { pref.Value = EditorGUILayout.IntField(pref.Content, pref.Value); } public static void DoGUI(this PrefItem pref) { pref.Value = EditorGUILayout.FloatField(pref.Content, pref.Value); } public static void DoGUI(this PrefItem pref, int min, int max) { pref.Value = EditorGUILayout.IntSlider(pref.Content, pref.Value, min, max); } public static void DoGUI(this PrefItem pref, float min, float max) { pref.Value = EditorGUILayout.Slider(pref.Content, pref.Value, min, max); } public static void DoGUI(this PrefItem pref) { pref.Value = EditorGUILayout.Toggle(pref.Content, pref.Value); } public static void DoGUI(this PrefItem pref) { pref.Value = EditorGUILayout.TextField(pref.Content, pref.Value); } public static void DoGUI(this PrefItem pref) { pref.Value = EditorGUILayout.ColorField(pref.Content, pref.Value); } public static void DoGUI(this PrefItem pref) { pref.Value = EditorGUILayout.RectField(pref.Content, pref.Value); } public static void DoGUI(this PrefItem pref) { pref.Value = (RectSourceMode)EditorGUILayout.EnumPopup(pref.Content, pref.Value); } } }