// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using UnityEditor; using UnityEngine; namespace Microsoft.MixedReality.Toolkit.Editor { /// /// Custom property drawer to show an optionally collapsible foldout help section in the Inspector /// /// /// /// [Help("This is a multiline optionally collapsable help section.\n • Great for providing simple instructions in Inspector.\n • Easy to use.\n • Saves space.")] /// /// [CustomPropertyDrawer(typeof(HelpAttribute))] public class HelpDrawer : DecoratorDrawer { /// /// Unity calls this function to draw the GUI /// /// Rectangle to display the GUI in public override void OnGUI(Rect position) { HelpAttribute help = attribute as HelpAttribute; if (help.Collapsible) { HelpFoldOut = EditorGUI.Foldout(position, HelpFoldOut, help.Header); if (HelpFoldOut) { EditorGUI.HelpBox(position, help.Text, MessageType.Info); } } else { EditorGUI.HelpBox(position, help.Text, MessageType.Info); } cachedPosition = position; } /// /// Gets the height of the decorator /// public override float GetHeight() { HelpAttribute help = attribute as HelpAttribute; // Computing the actual height requires the cachedPosition because // CalcSize doesn't factor in word-wrapped height, and CalcHeight // requires a pre-determined width. GUIStyle helpStyle = EditorStyles.helpBox; GUIContent helpContent = new GUIContent(help.Text); float wrappedHeight = helpStyle.CalcHeight(helpContent, cachedPosition.width); // The height of the help box should be the content if expanded, or // just the header text if not expanded. float contentHeight = !help.Collapsible || HelpFoldOut ? wrappedHeight : helpStyle.lineHeight; return helpStyle.margin.top + helpStyle.margin.bottom + contentHeight; } #region Private /// /// The "help" foldout state /// private bool HelpFoldOut = false; private Rect cachedPosition = new Rect(); #endregion } }