Skip to content

Commit 9e677d1

Browse files
committed
Possibility to define parent type for the [ReferencePicker] attribute
1 parent b256f6a commit 9e677d1

4 files changed

Lines changed: 49 additions & 7 deletions

File tree

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ public class ReferencePickerAttributeDrawer : ToolboxSelfPropertyDrawer<Referenc
1313
private readonly TypeField typeField = new TypeField(new TypeConstraintReference(null));
1414

1515

16-
private void CreateTypeProperty(SerializedProperty property)
16+
private void CreateTypeProperty(SerializedProperty property, Type parentType)
1717
{
18-
property.GetFieldInfo(out Type propertyType);
1918
TypeUtilities.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFullTypename, out var currentType);
2019
var position = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
2120
position = EditorGUI.IndentedRect(position);
@@ -44,7 +43,7 @@ private void CreateTypeProperty(SerializedProperty property)
4443
{
4544
ToolboxEditorLog.LogWarning("Invalid attempt to update disposed property.");
4645
}
47-
}, currentType, propertyType);
46+
}, currentType, parentType);
4847
}
4948

5049
private void UpdateTypeProperty(SerializedProperty property, Type referenceType)
@@ -55,6 +54,24 @@ private void UpdateTypeProperty(SerializedProperty property, Type referenceType)
5554
property.serializedObject.ApplyModifiedProperties();
5655
}
5756

57+
private Type GetParentType(SerializedProperty property, ReferencePickerAttribute attribute)
58+
{
59+
property.GetFieldInfo(out Type propertyType);
60+
var candidateType = attribute.ParentType;
61+
if (candidateType != null)
62+
{
63+
if (propertyType.IsAssignableFrom(candidateType))
64+
{
65+
return candidateType;
66+
}
67+
68+
ToolboxEditorLog.AttributeUsageWarning(attribute, property,
69+
$"Provided {nameof(attribute.ParentType)} ({candidateType}) cannot be used because it's not assignable from: '{propertyType}'");
70+
}
71+
72+
return propertyType;
73+
}
74+
5875

5976
protected override void OnGuiSafe(SerializedProperty property, GUIContent label, ReferencePickerAttribute attribute)
6077
{
@@ -66,7 +83,8 @@ protected override void OnGuiSafe(SerializedProperty property, GUIContent label,
6683
}
6784

6885
EditorGUI.indentLevel++;
69-
CreateTypeProperty(property);
86+
var parentType = GetParentType(property, attribute);
87+
CreateTypeProperty(property, parentType);
7088
ToolboxEditorGui.DrawPropertyChildren(property);
7189
EditorGUI.indentLevel--;
7290
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ namespace UnityEngine
1212
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
1313
[Conditional("UNITY_EDITOR")]
1414
public class ReferencePickerAttribute : ToolboxSelfPropertyAttribute
15-
{ }
15+
{
16+
public ReferencePickerAttribute()
17+
{ }
18+
19+
public ReferencePickerAttribute(Type parentType)
20+
{
21+
ParentType = parentType;
22+
}
23+
24+
public Type ParentType { get; set; }
25+
}
1626
}
1727
#endif

Assets/Examples/Scenes/SampleScene.unity

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,20 +581,26 @@ MonoBehaviour:
581581
id: 0
582582
var2:
583583
id: 1
584+
var3:
585+
id: 2
584586
references:
585587
version: 1
586588
00000000:
587589
type: {class: SampleBehaviour6/ClassWithInterface1, ns: , asm: Assembly-CSharp}
588590
data:
589591
go: {fileID: 977748987}
590592
var1:
591-
id: 2
593+
id: 3
592594
00000001:
595+
type: {class: SampleBehaviour6/ClassWithInterface3, ns: , asm: Assembly-CSharp}
596+
data:
597+
var1: 31
598+
00000002:
593599
type: {class: SampleBehaviour6/ClassWithInterface2, ns: , asm: Assembly-CSharp}
594600
data:
595601
var1: 0
596602
mat: {fileID: 2100000, guid: 7404c70251f9d0045a4aabaa49d83963, type: 2}
597-
00000002:
603+
00000003:
598604
type: {class: SampleBehaviour6/Struct, ns: , asm: Assembly-CSharp}
599605
data:
600606
var1: 1

Assets/Examples/Scripts/SampleBehaviour6.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class SampleBehaviour6 : MonoBehaviour
1111
public Interface1 var1;
1212
[SerializeReference, ReferencePicker]
1313
public ClassWithInterfaceBase var2;
14+
[SerializeReference, ReferencePicker(ParentType = typeof(ClassWithInterface2))]
15+
public ClassWithInterfaceBase var3;
1416
#endif
1517

1618
public interface Interface1 { }
@@ -49,4 +51,10 @@ public class ClassWithInterface3 : ClassWithInterfaceBase
4951
{
5052
public int var1;
5153
}
54+
55+
[Serializable]
56+
public class ClassWithInterface4 : ClassWithInterface2
57+
{
58+
public int var33;
59+
}
5260
}

0 commit comments

Comments
 (0)