|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using UnityEngine;
|
4 | 4 | using UnityEditor;
|
| 5 | +using UnityEditor.AnimatedValues; |
5 | 6 |
|
6 | 7 | [CustomPropertyDrawer(typeof(DeveloperDescription))]
|
7 | 8 | public class DeveloperDescriptionDrawer : PropertyDrawer
|
8 | 9 | {
|
9 | 10 | public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
10 | 11 | {
|
11 |
| - SerializedProperty stringValue = property.FindPropertyRelative("_value"); |
12 |
| - |
13 |
| - return Styles.TextAreaStyle.CalcSize(new GUIContent("Descriptions\n" + stringValue.stringValue)).y; |
| 12 | + return GetHeight(property); |
14 | 13 | }
|
15 | 14 | public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
16 | 15 | {
|
17 |
| - DrawTitle(ref position); |
| 16 | + DrawTitle(ref position, property); |
18 | 17 | DrawTextArea(ref position, property);
|
19 | 18 |
|
20 | 19 | property.serializedObject.ApplyModifiedProperties();
|
21 | 20 | }
|
22 |
| - private void DrawTitle(ref Rect rect) |
| 21 | + private void DrawTitle(ref Rect rect, SerializedProperty property) |
23 | 22 | {
|
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 | + } |
25 | 27 |
|
26 |
| - rect.y += EditorGUIUtility.singleLineHeight; |
| 28 | + EditorGUI.LabelField(rect, new GUIContent("Description", "Click below this field to add a description")); |
27 | 29 | }
|
28 | 30 | private void DrawTextArea(ref Rect rect, SerializedProperty property)
|
29 | 31 | {
|
30 | 32 | 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; |
34 | 38 |
|
35 | 39 | EditorGUI.indentLevel++;
|
36 | 40 | stringValue.stringValue = EditorGUI.TextArea(rect, stringValue.stringValue, Styles.TextAreaStyle);
|
@@ -64,6 +68,29 @@ private void Repaint(SerializedProperty property)
|
64 | 68 | {
|
65 | 69 | EditorUtility.SetDirty(property.serializedObject.targetObject);
|
66 | 70 | }
|
| 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 | + } |
67 | 94 | private static class Styles
|
68 | 95 | {
|
69 | 96 | public static GUIStyle TextAreaStyle;
|
|
0 commit comments