Skip to content

Commit 6fdb939

Browse files
committed
Possibility to specify TypeGrouping for Reference pickers
1 parent 95684b4 commit 6fdb939

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ namespace Toolbox.Editor.Drawers
1010

1111
public class ReferencePickerAttributeDrawer : ToolboxSelfPropertyDrawer<ReferencePickerAttribute>
1212
{
13-
private readonly TypeField typeField = new TypeField(new TypeConstraintReference(null));
13+
private static readonly TypeConstraintContext sharedConstraint = new TypeConstraintReference(null);
14+
private static readonly TypeAppearanceContext sharedAppearance = new TypeAppearanceContext(sharedConstraint, TypeGrouping.None, true);
15+
private static readonly TypeField typeField = new TypeField(sharedConstraint, sharedAppearance);
1416

1517

18+
private void UpdateContexts(ReferencePickerAttribute attribute)
19+
{
20+
sharedAppearance.TypeGrouping = attribute.TypeGrouping;
21+
}
22+
1623
private void CreateTypeProperty(SerializedProperty property, Type parentType)
1724
{
1825
TypeUtilities.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFullTypename, out var currentType);
@@ -72,7 +79,6 @@ private Type GetParentType(SerializedProperty property, ReferencePickerAttribute
7279
return propertyType;
7380
}
7481

75-
7682
protected override void OnGuiSafe(SerializedProperty property, GUIContent label, ReferencePickerAttribute attribute)
7783
{
7884
using (var propertyScope = new PropertyScope(property, label))
@@ -82,8 +88,10 @@ protected override void OnGuiSafe(SerializedProperty property, GUIContent label,
8288
return;
8389
}
8490

85-
EditorGUI.indentLevel++;
91+
UpdateContexts(attribute);
8692
var parentType = GetParentType(property, attribute);
93+
94+
EditorGUI.indentLevel++;
8795
CreateTypeProperty(property, parentType);
8896
ToolboxEditorGui.DrawPropertyChildren(property);
8997
EditorGUI.indentLevel--;

Assets/Editor Toolbox/Runtime/Attributes/Toolbox/PropertySelfAttributes/ReferencePickerAttribute.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,21 @@ public class ReferencePickerAttribute : ToolboxSelfPropertyAttribute
1616
public ReferencePickerAttribute()
1717
{ }
1818

19-
public ReferencePickerAttribute(Type parentType)
19+
public ReferencePickerAttribute(Type parentType) : this(parentType, TypeGrouping.None)
20+
{ }
21+
22+
public ReferencePickerAttribute(Type parentType, TypeGrouping typeGrouping)
2023
{
2124
ParentType = parentType;
25+
TypeGrouping = typeGrouping;
2226
}
2327

2428
public Type ParentType { get; set; }
29+
/// <summary>
30+
/// Gets or sets grouping of selectable classes.
31+
/// Defaults to <see cref="TypeGrouping.None"/> unless explicitly specified.
32+
/// </summary>
33+
public TypeGrouping TypeGrouping { get; set; } = TypeGrouping.None;
2534
}
2635
}
2736
#endif

Assets/Examples/Scripts/SampleBehaviour6.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class SampleBehaviour6 : MonoBehaviour
88
{
99
#if UNITY_2019_3_OR_NEWER
10-
[SerializeReference, ReferencePicker]
10+
[SerializeReference, ReferencePicker(TypeGrouping = TypeGrouping.ByFlatName)]
1111
public Interface1 var1;
1212
[SerializeReference, ReferencePicker]
1313
public ClassWithInterfaceBase var2;

0 commit comments

Comments
 (0)