Skip to content

Commit d74204f

Browse files
committed
Rewrite custom property drawers
1 parent 2e932c2 commit d74204f

File tree

5 files changed

+54
-122
lines changed

5 files changed

+54
-122
lines changed

Editor/AnimatorParameterPropertyDrawer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
1212
SerializedProperty hash = property.FindPropertyRelative("m_Hash");
1313

1414
EditorGUI.BeginProperty(position, label, property);
15-
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
1615

17-
string nameValue = EditorGUI.TextField(position, name.stringValue);
16+
string nameValue = EditorGUI.TextField(position, label, name.stringValue);
1817

1918
if (nameValue != name.stringValue)
2019
{
2120
name.stringValue = nameValue;
2221
hash.intValue = Animator.StringToHash(nameValue);
22+
property.serializedObject.ApplyModifiedProperties();
2323
}
2424

2525
EditorGUI.EndProperty();

Editor/Timing01PropertyDrawer.cs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,44 @@ namespace Zigurous.Animation.Editor
66
[CustomPropertyDrawer(typeof(Timing01))]
77
public sealed class Timing01PropertyDrawer : PropertyDrawer
88
{
9+
private static readonly GUIContent startLabel = new GUIContent("Start");
10+
private static readonly GUIContent endLabel = new GUIContent("End");
11+
912
private const float horizontalSpacing = 4f;
1013

1114
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
1215
{
13-
// Start drawing the property
1416
EditorGUI.BeginProperty(position, label, property);
1517

16-
// Draw the property label
1718
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
1819

19-
// Store orginal sizes so they can be set back to their original values
2020
int originalIndent = EditorGUI.indentLevel;
2121
float originalLabelWidth = EditorGUIUtility.labelWidth;
2222

23-
// Set indent level of child fields
2423
EditorGUI.indentLevel = 0;
2524

26-
// Create references to child fields
27-
SerializedProperty start = property.FindPropertyRelative("m_Start");
28-
SerializedProperty end = property.FindPropertyRelative("m_End");
25+
SerializedProperty m_Start = property.FindPropertyRelative("m_Start");
26+
SerializedProperty m_End = property.FindPropertyRelative("m_End");
2927

30-
// Calculate the bounds of the child fields
31-
Rect rect = new Rect(position);
32-
rect.width = (position.width - horizontalSpacing) / 2f;
28+
Rect field = new Rect(position);
29+
field.width = (position.width - horizontalSpacing) / 2f;
3330

34-
// Draw the child fields
35-
rect = SliderWithChangeCheck(start, rect, "Start");
36-
rect = SliderWithChangeCheck(end, rect, "End");
31+
field = SliderField(field, m_Start, startLabel);
32+
field = SliderField(field, m_End, endLabel);
3733

38-
// Set sizes back to their original values
3934
EditorGUI.indentLevel = originalIndent;
4035
EditorGUIUtility.labelWidth = originalLabelWidth;
41-
42-
// Finish drawing the property
4336
EditorGUI.EndProperty();
4437
}
4538

46-
private Rect SliderWithChangeCheck(SerializedProperty property, Rect position, string displayName)
39+
private Rect SliderField(Rect position, SerializedProperty property, GUIContent label)
4740
{
48-
// Check for changes in the field's value
49-
EditorGUI.BeginChangeCheck();
50-
51-
// Calculate the width of the field label
52-
GUIContent label = new GUIContent(displayName);
5341
EditorGUIUtility.labelWidth = EditorStyles.label.CalcSize(label).x;
5442

55-
// Draw the field and store the field's value
56-
float value = EditorGUI.Slider(position, label, property.floatValue, 0f, 1f);
57-
58-
// Update the property's value from the field
59-
if (EditorGUI.EndChangeCheck()) {
60-
property.floatValue = value;
61-
}
43+
property.serializedObject.Update();
44+
property.floatValue = EditorGUI.Slider(position, label, property.floatValue, 0f, 1f);
45+
property.serializedObject.ApplyModifiedProperties();
6246

63-
// Advance the position for the next child field
6447
position.x += position.width + horizontalSpacing;
6548
return position;
6649
}

Editor/TimingPropertyDrawer.cs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,44 @@ namespace Zigurous.Animation.Editor
66
[CustomPropertyDrawer(typeof(Timing))]
77
public sealed class TimingPropertyDrawer : PropertyDrawer
88
{
9+
private static readonly GUIContent startLabel = new GUIContent("Start");
10+
private static readonly GUIContent endLabel = new GUIContent("End");
11+
912
private const float horizontalSpacing = 4f;
1013

1114
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
1215
{
13-
// Start drawing the property
1416
EditorGUI.BeginProperty(position, label, property);
1517

16-
// Draw the property label
1718
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
1819

19-
// Store orginal sizes so they can be set back to their original values
2020
int originalIndent = EditorGUI.indentLevel;
2121
float originalLabelWidth = EditorGUIUtility.labelWidth;
2222

23-
// Set indent level of child fields
2423
EditorGUI.indentLevel = 0;
2524

26-
// Create references to child fields
27-
SerializedProperty start = property.FindPropertyRelative("m_Start");
28-
SerializedProperty end = property.FindPropertyRelative("m_End");
25+
SerializedProperty m_Start = property.FindPropertyRelative("m_Start");
26+
SerializedProperty m_End = property.FindPropertyRelative("m_End");
2927

30-
// Calculate the bounds of the child fields
31-
Rect rect = new Rect(position);
32-
rect.width = (position.width - horizontalSpacing) / 2f;
28+
Rect field = new Rect(position);
29+
field.width = (position.width - horizontalSpacing) / 2f;
3330

34-
// Draw the child fields
35-
rect = FloatFieldWithChangeCheck(start, rect);
36-
rect = FloatFieldWithChangeCheck(end, rect);
31+
field = FloatField(field, m_Start, startLabel);
32+
field = FloatField(field, m_End, endLabel);
3733

38-
// Set sizes back to their original values
3934
EditorGUI.indentLevel = originalIndent;
4035
EditorGUIUtility.labelWidth = originalLabelWidth;
41-
42-
// Finish drawing the property
4336
EditorGUI.EndProperty();
4437
}
4538

46-
private Rect FloatFieldWithChangeCheck(SerializedProperty property, Rect position)
39+
private Rect FloatField(Rect position, SerializedProperty property, GUIContent label)
4740
{
48-
// Check for changes in the field's value
49-
EditorGUI.BeginChangeCheck();
50-
51-
// Calculate the width of the field label
52-
GUIContent label = new GUIContent(property.displayName);
5341
EditorGUIUtility.labelWidth = EditorStyles.label.CalcSize(label).x;
5442

55-
// Draw the field and store the field's value
56-
float value = EditorGUI.FloatField(position, label, property.floatValue);
57-
58-
// Update the property's value from the field
59-
if (EditorGUI.EndChangeCheck()) {
60-
property.floatValue = value;
61-
}
43+
property.serializedObject.Update();
44+
property.floatValue = EditorGUI.FloatField(position, label, property.floatValue);
45+
property.serializedObject.ApplyModifiedProperties();
6246

63-
// Advance the position for the next child field
6447
position.x += position.width + horizontalSpacing;
6548
return position;
6649
}

Editor/TimingRange01PropertyDrawer.cs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,44 @@ namespace Zigurous.Animation.Editor
66
[CustomPropertyDrawer(typeof(TimingRange01))]
77
public sealed class TimingRange01PropertyDrawer : PropertyDrawer
88
{
9+
private static readonly GUIContent minLabel = new GUIContent("Min");
10+
private static readonly GUIContent maxLabel = new GUIContent("Max");
11+
912
private const float horizontalSpacing = 4f;
1013

1114
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
1215
{
13-
// Start drawing the property
1416
EditorGUI.BeginProperty(position, label, property);
1517

16-
// Draw the property label
1718
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
1819

19-
// Store orginal sizes so they can be set back to their original values
2020
int originalIndent = EditorGUI.indentLevel;
2121
float originalLabelWidth = EditorGUIUtility.labelWidth;
2222

23-
// Set indent level of child fields
2423
EditorGUI.indentLevel = 0;
2524

26-
// Create references to child fields
27-
SerializedProperty min = property.FindPropertyRelative("m_Min");
28-
SerializedProperty max = property.FindPropertyRelative("m_Max");
25+
SerializedProperty m_Min = property.FindPropertyRelative("m_Min");
26+
SerializedProperty m_Max = property.FindPropertyRelative("m_Max");
2927

30-
// Calculate the bounds of the child fields
31-
Rect rect = new Rect(position);
32-
rect.width = (position.width - horizontalSpacing) / 2f;
28+
Rect field = new Rect(position);
29+
field.width = (position.width - horizontalSpacing) / 2f;
3330

34-
// Draw the child fields
35-
rect = SliderWithChangeCheck(min, rect, "Min");
36-
rect = SliderWithChangeCheck(max, rect, "Max");
31+
field = SliderField(field, m_Min, minLabel);
32+
field = SliderField(field, m_Max, maxLabel);
3733

38-
// Set sizes back to their original values
3934
EditorGUI.indentLevel = originalIndent;
4035
EditorGUIUtility.labelWidth = originalLabelWidth;
41-
42-
// Finish drawing the property
4336
EditorGUI.EndProperty();
4437
}
4538

46-
private Rect SliderWithChangeCheck(SerializedProperty property, Rect position, string displayName)
39+
private Rect SliderField(Rect position, SerializedProperty property, GUIContent label)
4740
{
48-
// Check for changes in the field's value
49-
EditorGUI.BeginChangeCheck();
50-
51-
// Calculate the width of the field label
52-
GUIContent label = new GUIContent(displayName);
5341
EditorGUIUtility.labelWidth = EditorStyles.label.CalcSize(label).x;
5442

55-
// Draw the field and store the field's value
56-
float value = EditorGUI.Slider(position, label, property.floatValue, 0f, 1f);
57-
58-
// Update the property's value from the field
59-
if (EditorGUI.EndChangeCheck()) {
60-
property.floatValue = value;
61-
}
43+
property.serializedObject.Update();
44+
property.floatValue = EditorGUI.Slider(position, label, property.floatValue, 0f, 1f);
45+
property.serializedObject.ApplyModifiedProperties();
6246

63-
// Advance the position for the next child field
6447
position.x += position.width + horizontalSpacing;
6548
return position;
6649
}

Editor/TimingRangePropertyDrawer.cs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,44 @@ namespace Zigurous.Animation.Editor
66
[CustomPropertyDrawer(typeof(TimingRange))]
77
public sealed class TimingRangePropertyDrawer : PropertyDrawer
88
{
9+
private static readonly GUIContent minLabel = new GUIContent("Min");
10+
private static readonly GUIContent maxLabel = new GUIContent("Max");
11+
912
private const float horizontalSpacing = 4f;
1013

1114
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
1215
{
13-
// Start drawing the property
1416
EditorGUI.BeginProperty(position, label, property);
1517

16-
// Draw the property label
1718
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
1819

19-
// Store orginal sizes so they can be set back to their original values
2020
int originalIndent = EditorGUI.indentLevel;
2121
float originalLabelWidth = EditorGUIUtility.labelWidth;
2222

23-
// Set indent level of child fields
2423
EditorGUI.indentLevel = 0;
2524

26-
// Create references to child fields
27-
SerializedProperty min = property.FindPropertyRelative("m_Min");
28-
SerializedProperty max = property.FindPropertyRelative("m_Max");
25+
SerializedProperty m_Min = property.FindPropertyRelative("m_Min");
26+
SerializedProperty m_Max = property.FindPropertyRelative("m_Max");
2927

30-
// Calculate the bounds of the child fields
31-
Rect rect = new Rect(position);
32-
rect.width = (position.width - horizontalSpacing) / 2f;
28+
Rect field = new Rect(position);
29+
field.width = (position.width - horizontalSpacing) / 2f;
3330

34-
// Draw the child fields
35-
rect = FloatFieldWithChangeCheck(min, rect);
36-
rect = FloatFieldWithChangeCheck(max, rect);
31+
field = FloatField(field, m_Min, minLabel);
32+
field = FloatField(field, m_Max, maxLabel);
3733

38-
// Set sizes back to their original values
3934
EditorGUI.indentLevel = originalIndent;
4035
EditorGUIUtility.labelWidth = originalLabelWidth;
41-
42-
// Finish drawing the property
4336
EditorGUI.EndProperty();
4437
}
4538

46-
private Rect FloatFieldWithChangeCheck(SerializedProperty property, Rect position)
39+
private Rect FloatField(Rect position, SerializedProperty property, GUIContent label)
4740
{
48-
// Check for changes in the field's value
49-
EditorGUI.BeginChangeCheck();
50-
51-
// Calculate the width of the field label
52-
GUIContent label = new GUIContent(property.displayName);
5341
EditorGUIUtility.labelWidth = EditorStyles.label.CalcSize(label).x;
5442

55-
// Draw the field and store the field's value
56-
float value = EditorGUI.FloatField(position, label, property.floatValue);
57-
58-
// Update the property's value from the field
59-
if (EditorGUI.EndChangeCheck()) {
60-
property.floatValue = value;
61-
}
43+
property.serializedObject.Update();
44+
property.floatValue = EditorGUI.FloatField(position, label, property.floatValue);
45+
property.serializedObject.ApplyModifiedProperties();
6246

63-
// Advance the position for the next child field
6447
position.x += position.width + horizontalSpacing;
6548
return position;
6649
}

0 commit comments

Comments
 (0)