https://gist.github.com/CreatureSurvive/184e8fc9f39ac59c90aa81207425245e
(금지어가 링크 속 내용에 있어서 미리보기를 못 띄우네여)
유니티 기본 어트리뷰트 중 Space나 Header가 구분감이 약해서 커스텀 어트리뷰트를 찾았는데
원본 코드가 제 기준에는 조금 과해서 파티션 나누듯 구분하는 느낌으로 수정해봤어요.
위에 깃헙이 원본 코드고 제가 작성한건 아래입니다.
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace My
{
public class PartitionAttribute : PropertyAttribute
{
public string label;
public string tooltip;
/// <summary>
/// Add a partition
/// </summary>
public PartitionAttribute() { }
/// <summary>
/// Add a partition with a header
/// </summary>
/// <param name="label">A title for the header label</param>
/// <param name="tooltip">A note or instruction shown when hovering over the header</param>
public PartitionAttribute(string label = default, string tooltip = default) {
this.label = label;
this.tooltip = tooltip;
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(PartitionAttribute))]
public class PartitionDrawer : DecoratorDrawer
{
const float thickness = 4f;
const float lowerSpacing = 8f;
static readonly float lineHeight = EditorGUIUtility.singleLineHeight;
bool _hasLabel;
Color BackgroundColor => EditorGUIUtility.isProSkin ? new Color(0.18f, 0.18f, 0.18f, 0.75f) : new Color(0.82f, 0.82f, 0.82f, 0.75f);
GUIStyle LabelStyle => new(GUI.skin.label) { fontSize = 12, fontStyle = FontStyle.Bold, alignment = TextAnchor.MiddleLeft };
public override void OnGUI(Rect position) {
var attr = (attribute as PartitionAttribute);
var pos = EditorGUI.IndentedRect(position);
_hasLabel = attr.label != null;
EditorGUI.DrawRect(new Rect(pos.x - 20f, pos.y + 0.5f * (lineHeight - thickness) - 1f, pos.width + 24f, thickness), BackgroundColor);
if (_hasLabel) {
EditorGUI.LabelField(new Rect(pos.x, pos.y + lineHeight, pos.width - 16f, lineHeight), new GUIContent(attr.label, attr.tooltip), LabelStyle);
}
}
public override float GetHeight() {
return _hasLabel ? lineHeight + lineHeight + lowerSpacing : lineHeight;
}
}
#endif
}
}
사용은 위 세가지 방식으로 가능하고
- [Partition]을 쓴 경우
- [Partition(Header)]를 쓴 경우
폰트 크기랑 위치, 파티션 색깔 등은 코드에 수치 하나만 수정하면 쉽게 바꿀 수 있어요.
필요한 사람이 있을진 모르겠지만...
참고로 어트리뷰트 처음 공부할 때 원본코드에서 수치 이곳 저곳 만지면 유용할 듯 싶네요.
댓글 2