Last active
March 28, 2022 15:54
-
-
Save nomnomab/421fc0a6453efec7cf70ff699ba333a5 to your computer and use it in GitHub Desktop.
A custom title that is better than Unity's
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
namespace SLController.Attributes { | |
public class SimpleTitleAttribute: PropertyAttribute { | |
public readonly string Text; | |
public readonly string Style; | |
public SimpleTitleAttribute(string text, string style = null) { | |
Text = text; | |
Style = style; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using SLController.Attributes; | |
using UnityEditor; | |
using UnityEngine; | |
namespace SLController.Editor { | |
[CustomPropertyDrawer(typeof(SimpleTitleAttribute))] | |
public class SimpleTitleAttributeDrawer: DecoratorDrawer { | |
private const float HEIGHT = 12; | |
public override float GetHeight() { | |
SimpleTitleAttribute title = attribute as SimpleTitleAttribute; | |
GUIStyle style = title.Style ?? EditorStyles.boldLabel; | |
float fullTextHeight = style.CalcHeight(new GUIContent(title.Text), 1.0f); | |
int lines = 1; | |
if (title.Text != null) { | |
lines = title.Text.Count(a => a == '\n') + 1; | |
} | |
float eachLineHeight = fullTextHeight / lines; | |
return EditorGUIUtility.singleLineHeight * 1.5f + eachLineHeight * (lines - 1); | |
} | |
public override void OnGUI(Rect position) { | |
Rect titleRect = position; | |
SimpleTitleAttribute title = attribute as SimpleTitleAttribute; | |
EditorGUI.LabelField(titleRect, new GUIContent(title.Text), title.Style ?? EditorStyles.boldLabel); | |
float indent = EditorGUI.indentLevel * 16; | |
titleRect.height = 1; | |
titleRect.x += indent; | |
titleRect.width -= indent; | |
titleRect.y += HEIGHT + 10; | |
EditorGUI.DrawRect(titleRect, Color.gray); | |
} | |
} | |
} |
Author
nomnomab
commented
Mar 28, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment