Files
Cielonos/Assets/OtherPlugins/GraphicsCat/Modules/Common/PropertyAttributes/LabelDrawer.cs
SoulliesOfficial d15957c719 更新
2025-12-17 04:19:38 -05:00

73 lines
2.1 KiB
C#

#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
namespace GraphicsCat
{
[CustomPropertyDrawer(typeof(LabelAttribute))]
public class LabelDrawer : PropertyDrawer
{
private const float Spacing = 2f;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
float h0 = EditorGUIUtility.singleLineHeight;
float h1 = EditorGUI.GetPropertyHeight(property, label, true);
return h0 + Spacing + h1;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var attr = (LabelAttribute)attribute;
GUIStyle style = ResolveStyle(attr.Style);
Rect labelRect = new Rect(
position.x,
position.y,
position.width,
EditorGUIUtility.singleLineHeight
);
EditorGUI.LabelField(labelRect, attr.Text, style);
Rect fieldRect = new Rect(
position.x,
position.y + EditorGUIUtility.singleLineHeight + Spacing,
position.width,
EditorGUI.GetPropertyHeight(property, label, true)
);
EditorGUI.PropertyField(fieldRect, property, label, true);
}
private GUIStyle ResolveStyle(LabelStyle style)
{
switch (style)
{
case LabelStyle.Bold:
return EditorStyles.boldLabel;
case LabelStyle.Italic:
return EditorStyles.label.WithFontStyle(FontStyle.Italic);
case LabelStyle.BoldItalic:
return EditorStyles.label.WithFontStyle(FontStyle.BoldAndItalic);
default:
return EditorStyles.label;
}
}
}
internal static class GUIStyleExtensions
{
public static GUIStyle WithFontStyle(this GUIStyle style, FontStyle fontStyle)
{
GUIStyle s = new GUIStyle(style);
s.fontStyle = fontStyle;
return s;
}
}
}
#endif