// UltEvents // Copyright 2020 Kybernetik // #if UNITY_EDITOR using System; using UnityEditor; using UnityEngine; namespace UltEvents.Editor { /// [Editor-Only] /// A simple wrapper around to get and set a bool. /// /// If you are interested in a more comprehensive pref wrapper that supports more types, you should check out /// Inspector Gadgets. /// public sealed class BoolPref { /************************************************************************************************************************/ /// The text that will be displayed for this item in a context menu. public readonly string Label; /// The identifier with which this pref will be saved. public readonly string Key; /// The starting value to use for this pref if none was previously saved. public readonly bool DefaultValue; /// Called when the value is changed. public readonly Action OnChanged; /************************************************************************************************************************/ private bool _HasValue; private bool _Value; /// The current value of this pref. public bool Value { get { if (!_HasValue) { _HasValue = true; _Value = EditorPrefs.GetBool(Key, DefaultValue); } return _Value; } set { _Value = value; EditorPrefs.SetBool(Key, value); _HasValue = true; } } /// Returns the current value of the `pref`. public static implicit operator bool(BoolPref pref) { return pref.Value; } /************************************************************************************************************************/ /// Constructs a new . public BoolPref(string label, bool defaultValue, Action onChanged = null) { Label = label; Key = Names.Namespace + "." + label; DefaultValue = defaultValue; OnChanged = onChanged; _Value = EditorPrefs.GetBool(Key, defaultValue); } /************************************************************************************************************************/ /// Adds a menu item to toggle this pref. public void AddToMenu(GenericMenu menu) { menu.AddItem(new GUIContent("Display Options ->/" + Label), _Value, () => { _Value = !_Value; EditorPrefs.SetBool(Key, _Value); if (OnChanged != null) OnChanged(); }); } /************************************************************************************************************************/ /// Various settings. public static readonly BoolPref UseIndentation = new BoolPref("Use Indentation", true), AutoOpenMenu = new BoolPref("Auto Open Menu", true), AutoHideFooter = new BoolPref("Auto Hide Footer", true), ShowNonPublicMethods = new BoolPref("Show Non-Public Methods", true), GroupNonPublicMethods = new BoolPref("Group Non-Public Methods", true), ShowStaticMethods = new BoolPref("Show Static Methods", true), ShowFullTypeNames = new BoolPref("Use Full Type Names", false), GroupMethodOverloads = new BoolPref("Sub-Menu for Method Overloads", true), SubMenuForEachBaseType = new BoolPref("Base Types ->/Individual Sub-Menus", true, MethodSelectionMenu.ClearMemberCache), SubMenuForBaseTypes = new BoolPref("Base Types ->/Group Sub-Menu", true), SubMenuForRootBaseType = new BoolPref("Base Types ->/Group Root Type", false); /************************************************************************************************************************/ /// Adds menu items to toggle all prefs. public static void AddDisplayOptions(GenericMenu menu) { UseIndentation.AddToMenu(menu); AutoOpenMenu.AddToMenu(menu); AutoHideFooter.AddToMenu(menu); ShowNonPublicMethods.AddToMenu(menu); GroupNonPublicMethods.AddToMenu(menu); ShowStaticMethods.AddToMenu(menu); ShowFullTypeNames.AddToMenu(menu); GroupMethodOverloads.AddToMenu(menu); SubMenuForEachBaseType.AddToMenu(menu); SubMenuForBaseTypes.AddToMenu(menu); SubMenuForRootBaseType.AddToMenu(menu); } /************************************************************************************************************************/ } } #endif