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

Commit 174d153

Browse files
Refactored uses of GenericPropertyDrawer
1 parent 3538bad commit 174d153

5 files changed

Lines changed: 24 additions & 66 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private void DrawGenericPropertyField(Rect valueRect)
8787
{
8888
if (ValueType != null)
8989
{
90-
GenericPropertyDrawer.DrawPropertyDrawer(valueRect, ValueType, constantValue, GUIContent.none);
90+
GenericPropertyDrawer.DrawPropertyDrawer(valueRect, GUIContent.none, ValueType, constantValue, GUIContent.none);
9191
}
9292
else
9393
{

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

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,8 @@ namespace ScriptableObjectArchitecture.Editor
77
{
88
public static class GenericPropertyDrawer
99
{
10-
private const string VALUE_LABEL = "Value";
11-
12-
private static GUIContent ValueGUIContent
13-
{
14-
get
15-
{
16-
if (_valueGUIContent == null)
17-
{
18-
_valueGUIContent = new GUIContent(VALUE_LABEL);
19-
}
20-
21-
return _valueGUIContent;
22-
}
23-
}
24-
25-
private static GUIContent _valueGUIContent;
26-
27-
/// <summary>
28-
/// Draws a property drawer using the <see cref="EditorGUI"/> methods and the area the drawer is drawn
29-
/// in is determined by the passed <see cref="Rect"/> <paramref name="rect"/>.
30-
/// </summary>
31-
public static void DrawPropertyDrawer(Rect rect, Type type, SerializedProperty property, GUIContent errorLabel, bool drawValueLabel = false)
10+
public static void DrawPropertyDrawer(Rect rect, GUIContent label, Type type, SerializedProperty property, GUIContent errorLabel)
3211
{
33-
GUIContent label = drawValueLabel ? ValueGUIContent : GUIContent.none;
34-
3512
if (SOArchitecture_EditorUtility.HasPropertyDrawer(type) || typeof(Object).IsAssignableFrom(type) || type.IsEnum)
3613
{
3714
//Unity doesn't like it when you have scene objects on assets,
@@ -47,10 +24,9 @@ public static void DrawPropertyDrawer(Rect rect, Type type, SerializedProperty p
4724
}
4825
else if (type.IsAssignableFrom(typeof(Quaternion)))
4926
{
50-
property.quaternionValue = EditorGUI.Vector4Field(
51-
rect,
52-
label,
53-
property.quaternionValue.ToVector4()).ToQuaternion();
27+
Vector4 displayValue = property.quaternionValue.ToVector4();
28+
29+
property.quaternionValue = EditorGUI.Vector4Field(rect, label, displayValue).ToQuaternion();
5430
}
5531
else if (type.IsAssignableFrom(typeof(Vector4)))
5632
{
@@ -66,14 +42,9 @@ public static void DrawPropertyDrawer(Rect rect, Type type, SerializedProperty p
6642
EditorGUI.LabelField(rect, errorLabel);
6743
}
6844
}
69-
70-
/// <summary>
71-
/// Draws a property drawer using the <see cref="EditorGUILayout"/> methods.
72-
/// </summary>
73-
public static void DrawPropertyDrawerLayout(Type type, SerializedProperty property, GUIContent errorLabel, bool drawValueLabel = false)
45+
46+
public static void DrawPropertyDrawerLayout(Type type, GUIContent label, SerializedProperty property, GUIContent errorLabel)
7447
{
75-
GUIContent label = drawValueLabel ? ValueGUIContent : GUIContent.none;
76-
7748
if (SOArchitecture_EditorUtility.HasPropertyDrawer(type) || typeof(Object).IsAssignableFrom(type) || type.IsEnum)
7849
{
7950
//Unity doesn't like it when you have scene objects on assets,
@@ -89,9 +60,9 @@ public static void DrawPropertyDrawerLayout(Type type, SerializedProperty proper
8960
}
9061
else if (type.IsAssignableFrom(typeof(Quaternion)))
9162
{
92-
property.quaternionValue = EditorGUILayout.Vector4Field(
93-
label,
94-
property.quaternionValue.ToVector4()).ToQuaternion();
63+
Vector4 displayValue = property.quaternionValue.ToVector4();
64+
65+
property.quaternionValue = EditorGUILayout.Vector4Field(label, displayValue).ToQuaternion();
9566
}
9667
else if (type.IsAssignableFrom(typeof(Vector4)))
9768
{
@@ -107,16 +78,5 @@ public static void DrawPropertyDrawerLayout(Type type, SerializedProperty proper
10778
EditorGUILayout.LabelField(errorLabel);
10879
}
10980
}
110-
111-
/// <summary>
112-
/// Returns true if the passed <see cref="Type"/> <paramref name="type"/> should be displayed on
113-
/// single line regardless of whether <see cref="EditorGUI.GetPropertyHeight(SerializedProperty)"/>
114-
/// says otherwise.
115-
/// </summary>
116-
public static bool IsSingleLineGUIType(Type type)
117-
{
118-
return type.IsAssignableFrom(typeof(Vector4)) ||
119-
type.IsAssignableFrom(typeof(Quaternion));
120-
}
12181
}
12282
}

Assets/SO Architecture/Editor/Inspectors/BaseVariableEditor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected virtual void DrawValue()
5353
using (var scope = new EditorGUI.ChangeCheckScope())
5454
{
5555
string content = "Cannot display value. No PropertyDrawer for (" + Target.Type + ") [" + Target.ToString() + "]";
56-
GenericPropertyDrawer.DrawPropertyDrawerLayout(Target.Type, _valueProperty, new GUIContent(content, content), true);
56+
GenericPropertyDrawer.DrawPropertyDrawerLayout(Target.Type, new GUIContent("Value"), _valueProperty, new GUIContent(content, content));
5757

5858
if (scope.changed)
5959
{

Assets/SO Architecture/Editor/Inspectors/CollectionEditor.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ private void OnEnable()
5151
{
5252
drawHeaderCallback = DrawHeader,
5353
drawElementCallback = DrawElement,
54-
elementHeightCallback = GetElementHeight
5554
};
5655
}
5756
public override void OnInspectorGUI()
@@ -73,26 +72,14 @@ private void DrawHeader(Rect rect)
7372
}
7473
private void DrawElement(Rect rect, int index, bool isActive, bool isFocused)
7574
{
75+
rect = SOArchitecture_EditorUtility.GetReorderableListElementFieldRect(rect);
7676
SerializedProperty property = CollectionItemsProperty.GetArrayElementAtIndex(index);
7777

7878
EditorGUI.BeginDisabledGroup(DISABLE_ELEMENTS);
7979

80-
GenericPropertyDrawer.DrawPropertyDrawer(rect, Target.Type, property, _noPropertyDrawerWarningGUIContent);
80+
GenericPropertyDrawer.DrawPropertyDrawer(rect, new GUIContent("Element " + index), Target.Type, property, _noPropertyDrawerWarningGUIContent);
8181

8282
EditorGUI.EndDisabledGroup();
8383
}
84-
85-
/// <summary>
86-
/// Return the height of the item in the <see cref="ReorderableList"/> that is being drawn.
87-
/// </summary>
88-
/// <param name="index">The index of the item in the list being drawn.</param>
89-
/// <returns></returns>
90-
private float GetElementHeight(int index)
91-
{
92-
var indexedItemProperty = CollectionItemsProperty.GetArrayElementAtIndex(index);
93-
return GenericPropertyDrawer.IsSingleLineGUIType(Target.Type)
94-
? EditorGUIUtility.singleLineHeight
95-
: EditorGUI.GetPropertyHeight(indexedItemProperty);
96-
}
9784
}
9885
}

Assets/SO Architecture/Editor/SOArchitecture_EditorUtility.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ private static void CreateDebugStyle()
3636

3737
DebugStyle.normal.background = CreateTexture(2, 2, debugColor);
3838
}
39+
40+
/// <summary>
41+
/// Converts the entire rect of a <see cref="UnityEditorInternal.ReorderableList"/> element into a rect used for displaying a field
42+
/// </summary>
43+
public static Rect GetReorderableListElementFieldRect(Rect elementRect)
44+
{
45+
elementRect.height = EditorGUIUtility.singleLineHeight;
46+
elementRect.y++;
47+
48+
return elementRect;
49+
}
3950
public static bool SupportsMultiLine(Type type)
4051
{
4152
return type.GetCustomAttributes(typeof(MultiLine), true).Length > 0;

0 commit comments

Comments
 (0)