Custom Header (커스텀 헤더 : Color / FontSize)
Unity에서 Inspector의 제목을 설정할수있는 방법이다.
내장으로 사용하다보면 눈에 띄지 않는다.
Heade를 Custom 해서 좀더 눈에 잘띄도록 해보자.
Header를 커스텀해서 Size 와 Color, Align 기능을 추가했다.
Header를 여러 스크립트에서 사용하는 과정에서 좀더 눈에 띄고 좀더 다양하게 사용하기 위해서이다.
반응형
< 테스트 >
< HeaderDrawer.cs >
using UnityEngine;
using UnityEditor;
using System;
[CustomPropertyDrawer(typeof(HeaderAttribute))]
public class HeaderDrawer : DecoratorDrawer
{
public override void OnGUI(Rect position)
{
if (!(attribute is HeaderAttribute headerAttribute)) return;
if (string.IsNullOrEmpty(headerAttribute.header))
{
position.height = headerAttribute.textHeightIncrease;
EditorGUI.DrawRect(position, headerAttribute.color);
return;
}
position = EditorGUI.IndentedRect(position); //IndentRect First Check
GUIStyle style = new GUIStyle(EditorStyles.label) { richText = true };
GUIContent label = new GUIContent(
$"<color={headerAttribute.colorString}><size={style.fontSize + headerAttribute.textHeightIncrease}><b>[{headerAttribute.header}]</b></size></color>");
Vector2 textSize = style.CalcSize(label);
float separatorWidth = (position.width - textSize.x) / 2f;
float separatorStartY = position.yMin+ (position.height / 2f) - (headerAttribute.textHeightIncrease / 2f);
Rect prefixRect = new Rect();
Rect postRect = new Rect();
Rect labelRect;
float IndentedX = (position.x / 4f);
float labelWidth = (textSize.x + position.x);
switch (headerAttribute.headerAlign)
{
case HeaderAttribute.HeaderAlign.Center:
{
prefixRect = new Rect(position.xMin, separatorStartY, separatorWidth, headerAttribute.textHeightIncrease);
labelRect = new Rect(position.xMin + separatorWidth - IndentedX, position.yMin, labelWidth, position.height);
postRect = new Rect(position.xMin + separatorWidth + textSize.x, separatorStartY, separatorWidth, headerAttribute.textHeightIncrease);
}
break;
case HeaderAttribute.HeaderAlign.Left:
default:
{
labelRect = new Rect(position.xMin - IndentedX, position.yMin, labelWidth, position.height);
postRect = new Rect(position.xMin + textSize.x , separatorStartY, separatorWidth * 2f, headerAttribute.textHeightIncrease);
}
break;
case HeaderAttribute.HeaderAlign.Right:
{
prefixRect = new Rect(position.xMin , separatorStartY, separatorWidth * 2f, headerAttribute.textHeightIncrease);
labelRect = new Rect(prefixRect.width + position.xMin - IndentedX, position.yMin, labelWidth, position.height);
}
break;
}
EditorGUI.DrawRect(prefixRect, headerAttribute.color);
EditorGUI.LabelField(labelRect, label, style);
EditorGUI.DrawRect(postRect, headerAttribute.color);
}
/// <summary>
/// 높이 기본값 1.5배로 처리
/// </summary>
/// <returns></returns>
public override float GetHeight()
{
if ((attribute is HeaderAttribute headerAttribute) == false)
return EditorGUIUtility.singleLineHeight * 1.5f;
return GetTotalHeight(attribute as HeaderAttribute);
}
/// <summary>
/// 줄바꿈 계산
/// </summary>
/// <param name="attr"></param>
/// <returns></returns>
private float GetTotalHeight(HeaderAttribute attr)
{
int line =(attr.header.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries).Length );
return (EditorGUIUtility.singleLineHeight * Mathf.Max(line, 1)) + (EditorGUIUtility.singleLineHeight * 0.5f);
}
}
< HeaderAttribute.cs >
using System;
using UnityEngine;
[System.AttributeUsage(AttributeTargets.Field, Inherited =true, AllowMultiple =true)]
public class HeaderAttribute : PropertyAttribute
{
public readonly string header;
public readonly string colorString;
public readonly Color color;
public readonly float textHeightIncrease;
public readonly HeaderAlign headerAlign = HeaderAlign.Left;
private const float DefaultSize = 2f;
public const float HeaderMaxSize = 8f;
public enum HeaderAlign
{
Left,
Center,
Right,
}
public HeaderAttribute(string header)
{
this.header = header;
this.textHeightIncrease = DefaultSize;
}
public HeaderAttribute(string header, int order)
{
this.header = header;
this.textHeightIncrease = DefaultSize;
this.order = order;
}
public HeaderAttribute(string header, string colorString) : this(header, DefaultSize, colorString)
{
}
public HeaderAttribute(string header, HeaderAlign headerAlign) : this(header, DefaultSize, "white")
{
this.headerAlign = headerAlign;
}
public HeaderAttribute(string header, string colorString, HeaderAlign headerAlign) : this(header, DefaultSize, colorString)
{
this.headerAlign = headerAlign;
}
public HeaderAttribute(string header, float textHeightIncrease, string colorString, HeaderAlign headerAlign ) : this(header, textHeightIncrease, colorString)
{
this.headerAlign = headerAlign;
}
public HeaderAttribute(string header, float textHeightIncrease, string colorString, HeaderAlign headerAlign, int order) : this(header, textHeightIncrease, colorString)
{
this.headerAlign = headerAlign;
this.order = order;
}
public HeaderAttribute(string header, float textHeightIncrease = DefaultSize, string colorString = "lightblue")
{
this.header = header;
this.colorString = colorString;
//Size Range
float size = Mathf.Max(DefaultSize, textHeightIncrease);
this.textHeightIncrease = Mathf.Min(size, HeaderMaxSize);
if (string.IsNullOrEmpty(header))
this.textHeightIncrease = DefaultSize;
if (ColorUtility.TryParseHtmlString(colorString, out this.color)) return;
this.color = new Color(173f, 216f, 230f);
this.colorString = "lightblue";
}
}
헤더를 커스텀해서 좀더 눈에 잘띄는 형태로 수정했다.
List에서도 처리될수 있도록 EditorGUI.IndentedRect(position); 를 처리했고
헤더의 Text 크기, 정렬, 컬러등 간략하게 사용 가능하도록 수정했다.
Unity Header : [링크]
★☆☆☆☆
반응형
'개발 > Unity' 카테고리의 다른 글
Unity) Book Page Curl (책 넘기는 효과) (0) | 2022.06.06 |
---|---|
Unity) Unity 에서 Android Callback 처리 방법 (0) | 2022.06.02 |
Unity) Inspector HelpBox(경고표시, 정보표시, 위험표시) (0) | 2022.05.25 |
Unity)Firebase) Cloud Messaging (FCM - Notification / Push Messasge) (0) | 2022.05.24 |
Unity)코드) 문자열 줄바꿈(개행문자) 계산 (0) | 2022.05.18 |
댓글