更新日 2022/7/16
【Unity】メッセージボックスを表示するエディター拡張方法を紹介します!
メッセージボックスを表示するエディター拡張方法を紹介します。

Message属性を作成する
メッセージボックス表示用の属性を作成します。
MessageAttribute.cs
using System;
namespace UnityEngine
{
[AttributeUsage(AttributeTargets.Field)]
public class MessageAttribute : PropertyAttribute
{
public readonly int type;
public readonly int height;
public MessageAttribute(int type, int height)
{
this.type = type;
this.height = height;
}
}
}
typeはUnityEditor.MessageTypeの整数値です。
heightは表示するボックスの高さです。
MessageDrawerを作成する
HelpBox表示するMessageDrawerクラスを作成します。
MessageDrawer.cs
using UnityEngine;
namespace UnityEditor
{
[CustomPropertyDrawer(typeof(MessageAttribute))]
public class MessageDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var message = attribute as MessageAttribute;
EditorGUI.HelpBox(position, property.stringValue, (MessageType)message.type);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
var message = attribute as MessageAttribute;
return message.height;
}
}
}
メッセージボックスはEditorGUI.HelpBoxで表示します。
高さを属性で指定できるようにしたのでGetPropertyHeightをオーバーライドします。
メッセージボックスを表示する
メッセージボックスを表示するMonoBehaviorを作成します。
第1引数はUnityEditor.MessageTypeの整数値で、
第2引数はメッセージボックスの高さです。
using UnityEngine;
public class TestScript : MonoBehaviour
{
[Message(0, 30)]
public string message = "アイコンなし\n\\nで改行できます。";
[Message(1, 40)]
public string info = "情報\n\\nで改行できます。";
[Message(2, 50)]
public string warning = "警告\n\\nで改行できます。";
[Message(3, 60)]
public string error = "エラー\n\\nで改行できます。";
}

メッセージボックスを表示するエディター拡張を紹介しました。
アクセントを付けたいときに使用してみてはいかがでしょうか。
関連ページ
こちらのページも合わせてご覧下さい。

【Unity】エディター拡張方法をまとめます!2022/9/21

【Unity】Vector4を1行で表示するエディター拡張を紹介します!2022/7/6

【Unity】両端編集可能なスライダーを表示するエディター拡張を紹介します!2022/7/11

【Unity】ツールバー表示するエディター拡張方法を紹介します!2022/7/14

【Unity】プログレスバーを表示するエディター拡張方法を紹介します!2022/7/15