Skip to content

Instantly share code, notes, and snippets.

@baba-s
Last active September 13, 2018 00:44
Show Gist options
  • Select an option

  • Save baba-s/f0d0bb59e0d39724a885 to your computer and use it in GitHub Desktop.

Select an option

Save baba-s/f0d0bb59e0d39724a885 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
/// <summary>
/// ヘルプボックスの種類
/// </summary>
public enum HelpBoxType
{
/// <summary>
/// 通常
/// </summary>
None,
/// <summary>
/// 情報
/// </summary>
Info,
/// <summary>
/// 警告
/// </summary>
Warning,
/// <summary>
/// エラー
/// </summary>
Error,
}
/// <summary>
/// Inspector ビューに HelpBox を表示します
/// </summary>
/// <example>
/// <code>
/// [HelpBox( "通常メッセージ", HelpBoxType.None, 0 )]\n
/// [HelpBox( "情報メッセージ", HelpBoxType.Info, 1 )]\n
/// [HelpBox( "警告メッセージ", HelpBoxType.Warning, 2 )]\n
/// [HelpBox( "エラーメッセージ", HelpBoxType.Error, 3 )]\n
/// </code>
/// </example>
[AttributeUsage( AttributeTargets.Field, Inherited = true, AllowMultiple = true )]
public sealed class HelpBoxAttribute : PropertyAttribute
{
/// <summary>
/// 表示するメッセージ
/// </summary>
public string Message;
/// <summary>
/// メッセージのタイプ
/// </summary>
public HelpBoxType Type;
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="message">表示するメッセージ</param>
/// <param name="type">メッセージのタイプ</param>
/// <param name="order">表示順位</param>
public HelpBoxAttribute( string message, HelpBoxType type = HelpBoxType.None, int order = 0 )
{
Message = message;
Type = type;
this.order = order;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer( typeof( HelpBoxAttribute ) )]
public sealed class HelpBoxDrawer : DecoratorDrawer
{
private HelpBoxAttribute HelpBoxAttribute { get { return attribute as HelpBoxAttribute; } }
public override void OnGUI( Rect position )
{
var helpBoxPosition = EditorGUI.IndentedRect( position );
helpBoxPosition.height = GetHelpBoxHeight();
EditorGUI.HelpBox( helpBoxPosition, HelpBoxAttribute.Message, GetMessageType( HelpBoxAttribute.Type ) );
}
public override float GetHeight()
{
return GetHelpBoxHeight();
}
public MessageType GetMessageType( HelpBoxType type )
{
switch ( type )
{
case HelpBoxType.Error: return MessageType.Error;
case HelpBoxType.Info: return MessageType.Info;
case HelpBoxType.None: return MessageType.None;
case HelpBoxType.Warning: return MessageType.Warning;
}
return 0;
}
public float GetHelpBoxHeight()
{
var style = new GUIStyle( "HelpBox" );
var content = new GUIContent( HelpBoxAttribute.Message );
return Mathf.Max( style.CalcHeight( content, Screen.width - ( HelpBoxAttribute.Type != HelpBoxType.None ? 53 : 21) ), 40);
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment