Skip to content

Commit 903cfe5

Browse files
committed
WIP drawing improvements for the ReferencePicker
1 parent 05d27c2 commit 903cfe5

3 files changed

Lines changed: 25 additions & 12 deletions

File tree

Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelf/ReferencePickerAttributeDrawer.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Toolbox.Editor.Drawers
1010

1111
public class ReferencePickerAttributeDrawer : ToolboxSelfPropertyDrawer<ReferencePickerAttribute>
1212
{
13+
private const float minTypeFieldWidth = 30.0f;
14+
1315
private static readonly TypeConstraintContext sharedConstraint = new TypeConstraintReference(null);
1416
private static readonly TypeAppearanceContext sharedAppearance = new TypeAppearanceContext(sharedConstraint, TypeGrouping.None, true);
1517
private static readonly TypeField typeField = new TypeField(sharedConstraint, sharedAppearance);
@@ -20,11 +22,9 @@ private void UpdateContexts(ReferencePickerAttribute attribute)
2022
sharedAppearance.TypeGrouping = attribute.TypeGrouping;
2123
}
2224

23-
private void CreateTypeProperty(SerializedProperty property, Type parentType)
25+
private void CreateTypeProperty(Rect position, SerializedProperty property, Type parentType)
2426
{
2527
TypeUtilities.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFullTypename, out var currentType);
26-
var position = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
27-
position = EditorGUI.IndentedRect(position);
2828
typeField.OnGui(position, true, (type) =>
2929
{
3030
try
@@ -83,17 +83,24 @@ protected override void OnGuiSafe(SerializedProperty property, GUIContent label,
8383
{
8484
using (var propertyScope = new PropertyScope(property, label))
8585
{
86-
if (!propertyScope.IsVisible)
87-
{
88-
return;
89-
}
90-
9186
UpdateContexts(attribute);
9287
var parentType = GetParentType(property, attribute);
93-
9488
EditorGUI.indentLevel++;
95-
CreateTypeProperty(property, parentType);
96-
ToolboxEditorGui.DrawPropertyChildren(property);
89+
90+
var position = GUILayoutUtility.GetLastRect();
91+
position.xMax = position.xMin + EditorGUIUtility.currentViewWidth;
92+
var labelSize = EditorStyles.label.CalcSize(label);
93+
var x = Mathf.Max(EditorGUIUtility.labelWidth, labelSize.x);
94+
position.xMin = position.xMin + x;
95+
//position = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
96+
//position = EditorGUI.IndentedRect(position);
97+
98+
CreateTypeProperty(position, property, parentType);
99+
if (propertyScope.IsVisible)
100+
{
101+
ToolboxEditorGui.DrawPropertyChildren(property);
102+
}
103+
97104
EditorGUI.indentLevel--;
98105
}
99106
}

Assets/Editor Toolbox/Editor/Internal/PropertyScope.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ private void TryDrawLabel(Rect rect, GUIContent label)
3535
{
3636
if (property.hasChildren)
3737
{
38+
var size = EditorStyles.label.CalcSize(label);
39+
size.x = Mathf.Max(16, size.x);
40+
rect.xMax = rect.xMin + size.x;
3841
property.isExpanded = EditorGUI.Foldout(rect, property.isExpanded, label, true);
3942
}
4043
else
@@ -52,4 +55,4 @@ public void Dispose()
5255

5356
public bool IsVisible => property.isExpanded;
5457
}
55-
}
58+
}

Assets/Examples/Scripts/SampleBehaviour6.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public class SampleBehaviour6 : MonoBehaviour
1313
public ClassWithInterfaceBase var2;
1414
[SerializeReference, ReferencePicker(ParentType = typeof(ClassWithInterface2))]
1515
public ClassWithInterfaceBase var3;
16+
public int a;
17+
[SerializeField, SerializeReference, ReferencePicker]
18+
public Interface1[] vars;
1619
#endif
1720

1821
public interface Interface1 { }

0 commit comments

Comments
 (0)