Skip to content

Commit

Permalink
Button and ReorderableList + Label support rich text
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrizov committed Jan 11, 2021
1 parent 473fb3b commit aa20fa3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public sealed override void OnGUI(Rect rect, SerializedProperty property, GUICon

using (new EditorGUI.DisabledScope(disabled: !enabled))
{
OnGUI_Internal(rect, property, new GUIContent(PropertyUtility.GetLabel(property)));
OnGUI_Internal(rect, property, PropertyUtility.GetLabel(property));
}

// Call OnValueChanged callbacks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ public class ReorderableListPropertyDrawer : SpecialCasePropertyDrawerBase

private readonly Dictionary<string, ReorderableList> _reorderableListsByPropertyName = new Dictionary<string, ReorderableList>();

private GUIStyle _labelStyle;

private GUIStyle GetLabelStyle()
{
if (_labelStyle == null)
{
_labelStyle = new GUIStyle(EditorStyles.boldLabel);
_labelStyle.richText = true;
}

return _labelStyle;
}

private string GetPropertyKeyName(SerializedProperty property)
{
return property.serializedObject.targetObject.GetInstanceID() + "." + property.name;
Expand Down Expand Up @@ -46,7 +59,7 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G
{
drawHeaderCallback = (Rect r) =>
{
EditorGUI.LabelField(r, string.Format("{0}: {1}", label.text, property.arraySize), EditorStyles.boldLabel);
EditorGUI.LabelField(r, string.Format("{0}: {1}", label.text, property.arraySize), GetLabelStyle());
HandleDragAndDrop(r, reorderableList);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void OnGUI(Rect rect, SerializedProperty property)

using (new EditorGUI.DisabledScope(disabled: !enabled))
{
OnGUI_Internal(rect, property, new GUIContent(PropertyUtility.GetLabel(property)));
OnGUI_Internal(rect, property, PropertyUtility.GetLabel(property));
}

// Call OnValueChanged callbacks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public static class NaughtyEditorGUI
public const float IndentLength = 15.0f;
public const float HorizontalSpacing = 2.0f;

private static GUIStyle _buttonStyle = new GUIStyle(GUI.skin.button) { richText = true };

private delegate void PropertyFieldFunction(Rect rect, SerializedProperty property, GUIContent label, bool includeChildren);

public static void PropertyField(Rect rect, SerializedProperty property, bool includeChildren)
Expand Down Expand Up @@ -46,7 +48,7 @@ private static void PropertyField_Implementation(Rect rect, SerializedProperty p
}
else
{
GUIContent label = new GUIContent(PropertyUtility.GetLabel(property));
GUIContent label = PropertyUtility.GetLabel(property);
bool anyDrawerAttribute = PropertyUtility.GetAttributes<DrawerAttribute>(property).Any();

if (!anyDrawerAttribute)
Expand Down Expand Up @@ -171,7 +173,7 @@ public static void Button(UnityEngine.Object target, MethodInfo methodInfo)

EditorGUI.BeginDisabledGroup(!buttonEnabled);

if (GUILayout.Button(buttonText))
if (GUILayout.Button(buttonText, _buttonStyle))
{
object[] defaultParams = methodInfo.GetParameters().Select(p => p.DefaultValue).ToArray();
IEnumerator methodResult = methodInfo.Invoke(target, defaultParams) as IEnumerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ public static class PropertyUtility
return (T[])fieldInfo.GetCustomAttributes(typeof(T), true);
}

public static string GetLabel(SerializedProperty property)
public static GUIContent GetLabel(SerializedProperty property)
{
LabelAttribute labelAttribute = GetAttribute<LabelAttribute>(property);
return (labelAttribute == null)
string labelText = (labelAttribute == null)
? property.displayName
: labelAttribute.Label;

GUIContent label = new GUIContent(labelText);
return label;
}

public static void CallOnValueChangedCallbacks(SerializedProperty property)
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2020.2.0f1
m_EditorVersionWithRevision: 2020.2.0f1 (3721df5a8b28)
m_EditorVersion: 2020.2.1f1
m_EditorVersionWithRevision: 2020.2.1f1 (270dd8c3da1c)

0 comments on commit aa20fa3

Please sign in to comment.