はじめに
インスペクター上のフィールドにヘルプボックスを表示するアトリビュートを作成しました。
使用イメージ
[HelpBox(@"Help Box Help Box Help Box")] [SerializeField] private float m_Value;
ソースコード
HelpBoxAttribute
using System; using UnityEngine; [AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)] public class HelpBoxAttribute : PropertyAttribute { public readonly string Text; public HelpBoxAttribute(string text) { Text = text; } }
HelpBoxDrawer
using System; using UnityEditor; using UnityEngine; [CustomPropertyDrawer(typeof(HelpBoxAttribute))] public class HelpBoxDrawer : PropertyDrawer { private float m_Height; private HelpBoxAttribute m_HelpBoxAttribute => attribute as HelpBoxAttribute; public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var helpBoxAttribute = attribute as HelpBoxAttribute; EditorGUI.PropertyField(position, property, label, true); position = new Rect(0.0f, position.y + EditorGUI.GetPropertyHeight(property, label, true) + 5.0f, position.width, m_Height); EditorGUI.HelpBox(position, helpBoxAttribute.Text, MessageType.Info); } public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { var content = new GUIContent(m_HelpBoxAttribute.Text); m_Height = GUI.skin.box.CalcHeight(content, EditorGUIUtility.currentViewWidth); }