Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.

Commit 0643833

Browse files
Hide description header when content is available
1 parent 26faf5e commit 0643833

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

Assets/SO Architecture/Editor/Drawers/DeveloperDescriptionDrawer.cs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,39 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44
using UnityEditor;
5+
using UnityEditor.AnimatedValues;
56

67
[CustomPropertyDrawer(typeof(DeveloperDescription))]
78
public class DeveloperDescriptionDrawer : PropertyDrawer
89
{
910
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
1011
{
11-
SerializedProperty stringValue = property.FindPropertyRelative("_value");
12-
13-
return Styles.TextAreaStyle.CalcSize(new GUIContent("Descriptions\n" + stringValue.stringValue)).y;
12+
return GetHeight(property);
1413
}
1514
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
1615
{
17-
DrawTitle(ref position);
16+
DrawTitle(ref position, property);
1817
DrawTextArea(ref position, property);
1918

2019
property.serializedObject.ApplyModifiedProperties();
2120
}
22-
private void DrawTitle(ref Rect rect)
21+
private void DrawTitle(ref Rect rect, SerializedProperty property)
2322
{
24-
EditorGUI.LabelField(rect, new GUIContent("Description", "Click below this field to add a description"));
23+
if(HasContent(property))
24+
{
25+
rect.height = 0;
26+
}
2527

26-
rect.y += EditorGUIUtility.singleLineHeight;
28+
EditorGUI.LabelField(rect, new GUIContent("Description", "Click below this field to add a description"));
2729
}
2830
private void DrawTextArea(ref Rect rect, SerializedProperty property)
2931
{
3032
SerializedProperty stringValue = property.FindPropertyRelative("_value");
31-
32-
Vector2 fieldSize = Styles.TextAreaStyle.CalcSize(new GUIContent(stringValue.stringValue));
33-
rect.height = fieldSize.y;
33+
34+
if (!HasContent(property))
35+
rect.y += EditorGUIUtility.singleLineHeight;
36+
37+
rect.height = Styles.TextAreaStyle.CalcSize(new GUIContent(stringValue.stringValue)).y;
3438

3539
EditorGUI.indentLevel++;
3640
stringValue.stringValue = EditorGUI.TextArea(rect, stringValue.stringValue, Styles.TextAreaStyle);
@@ -64,6 +68,29 @@ private void Repaint(SerializedProperty property)
6468
{
6569
EditorUtility.SetDirty(property.serializedObject.targetObject);
6670
}
71+
private static bool HasContent(SerializedProperty property)
72+
{
73+
string content = GetContent(property);
74+
75+
return content != "" && content != string.Empty;
76+
}
77+
private static string GetContent(SerializedProperty property)
78+
{
79+
return property.FindPropertyRelative("_value").stringValue;
80+
}
81+
private static float GetHeight(SerializedProperty property)
82+
{
83+
string content = GetContent(property);
84+
85+
if (!HasContent(property))
86+
{
87+
return EditorGUIUtility.singleLineHeight * 2;
88+
}
89+
else
90+
{
91+
return Styles.TextAreaStyle.CalcSize(new GUIContent(content)).y;
92+
}
93+
}
6794
private static class Styles
6895
{
6996
public static GUIStyle TextAreaStyle;

0 commit comments

Comments
 (0)