Skip to content

Instantly share code, notes, and snippets.

@nomnomab
Last active March 28, 2022 15:54
Show Gist options
  • Save nomnomab/421fc0a6453efec7cf70ff699ba333a5 to your computer and use it in GitHub Desktop.
Save nomnomab/421fc0a6453efec7cf70ff699ba333a5 to your computer and use it in GitHub Desktop.
A custom title that is better than Unity's
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;
}
}
}
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);
}
}
}
@nomnomab
Copy link
Author

[SimpleTitle("Systems")]
[SerializeField] private SerializedSystems _updateLoopSystems;

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment