Skip to content

Instantly share code, notes, and snippets.

@giacomelli
Last active July 14, 2020 10:31
Show Gist options
  • Save giacomelli/be4850e86a0aacd3de4266830566b139 to your computer and use it in GitHub Desktop.
Save giacomelli/be4850e86a0aacd3de4266830566b139 to your computer and use it in GitHub Desktop.
#unitytips: HelpBox Attribute (with docs button)
using UnityEngine;
using System;
namespace Giacomelli.Framework
{
public enum HelpBoxType
{
/// <summary>
/// <para>Neutral message.</para>
/// </summary>
None,
/// <summary>
/// <para>Info message.</para>
/// </summary>
Info,
/// <summary>
/// <para>Warning message.</para>
/// </summary>
Warning,
/// <summary>
/// <para>Error message.</para>
/// </summary>
Error
}
[AttributeUsage(AttributeTargets.Field)]
public class HelpBoxAttribute : PropertyAttribute
{
public HelpBoxAttribute(string text, string docsUrl = null, HelpBoxType type = HelpBoxType.Info)
{
Text = text;
DocsUrl = docsUrl;
Type = type;
}
public string Text { get; }
public string DocsUrl { get; }
public HelpBoxType Type { get; }
}
}
using UnityEngine;
using UnityEditor;
using System;
namespace Giacomelli.Framework
{
[CustomPropertyDrawer(typeof(HelpBoxAttribute))]
public class HelpBoxDrawer : PropertyDrawer
{
const float XPadding = 30f;
const float YPadding = 5f;
const float DefaultHeight = 20f;
const float DocsButtonHeight = 20f;
float _height;
public override void OnGUI(Rect position,
SerializedProperty property,
GUIContent label)
{
var attr = attribute as HelpBoxAttribute;
CalculateHeight(attr);
EditorGUI.PropertyField(position, property, label, true);
position = new Rect(
XPadding,
position.y + EditorGUI.GetPropertyHeight(property, label, true) + YPadding,
position.width - XPadding,
_height);
EditorGUI.HelpBox(position, attr.Text, (MessageType) attr.Type);
if (!string.IsNullOrEmpty(attr.DocsUrl))
{
position = new Rect(
position.x + position.width - 40,
position.y + position.height - DocsButtonHeight,
40,
DocsButtonHeight);
if(GUI.Button(position, "Docs"))
{
if (attr.DocsUrl.StartsWith("http"))
Application.OpenURL(attr.DocsUrl);
else
Application.OpenURL($"https://docs.unity3d.com/ScriptReference/{attr.DocsUrl}");
}
}
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, true) + _height + 10;
}
void CalculateHeight(HelpBoxAttribute attr)
{
_height = (attr.Text.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries).Length + 1) * DefaultHeight;
if (!string.IsNullOrEmpty(attr.DocsUrl))
_height += DocsButtonHeight;
}
}
}
using Giacomelli.Framework;
using UnityEngine;
public class SampleComponent : MonoBehaviour
{
[SerializeField]
[HelpBox("Values are 0 for the primary button (often the left button), 1 for secondary button, and 2 for the middle button.", "Input.GetMouseButtonDown.html")]
int _button;
[SerializeField]
[HelpBox("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. esse cillum dolore eu fugiat nulla pariatur.", "http://diegogiacomelli.com.br/", HelpBoxType.Info)]
string _infoSample;
[SerializeField]
[HelpBox("Warning sample HelpBox with docs button", "http://diegogiacomelli.com.br/unitytips-helpbox-attribute/", HelpBoxType.Warning)]
string _warningSample;
[SerializeField]
[HelpBox("Error sample HelpBox with docs button", "http://diegogiacomelli.com.br/unitytips-helpbox-attribute/", HelpBoxType.Error)]
string _errorSample;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment