Files
trail-into-darkness/Packages/com.jovian.inspector/Editor/Attributes/InfoAttribute.cs
2026-03-29 18:59:24 +02:00

60 lines
2.0 KiB
C#

using UnityEngine;
namespace InspectorToolkit {
public class InfoAttribute : PropertyAttribute {
public enum MessageType {
Info,
Warning,
Error
}
#if UNITY_EDITOR
public readonly UnityEditor.MessageType messageType;
public readonly string message;
#endif
public InfoAttribute(string message, MessageType messageType = MessageType.Info) {
#if UNITY_EDITOR
this.message = message;
switch(messageType) {
case MessageType.Error:
this.messageType = UnityEditor.MessageType.Error;
break;
case MessageType.Warning:
this.messageType = UnityEditor.MessageType.Warning;
break;
case MessageType.Info:
default:
this.messageType = UnityEditor.MessageType.Info;
break;
}
#endif
}
}
}
#if UNITY_EDITOR
namespace InspectorToolkit.Internal {
using UnityEditor;
[CustomPropertyDrawer(typeof(InfoAttribute))]
public class InfoAttributeDrawer : PropertyDrawer {
private const float MESSAGEBOX_HEIGHT = 40f;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
return base.GetPropertyHeight(property, label) + MESSAGEBOX_HEIGHT;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
var message = ((InfoAttribute)attribute).message;
var messageType = ((InfoAttribute)attribute).messageType;
var boxPosition = new Rect(position.x, position.y, position.width, MESSAGEBOX_HEIGHT);
EditorGUI.HelpBox(boxPosition, message, messageType);
var fieldPosition = new Rect(position.x, position.y + MESSAGEBOX_HEIGHT, position.width, 18f);
EditorGUI.PropertyField(fieldPosition, property, label);
}
}
}
#endif