Skip to content

Commit 40caf54

Browse files
committed
Fix retrieving field type in the ReferencePickerAttributeDrawer; minor refactor changes
1 parent d215642 commit 40caf54

4 files changed

Lines changed: 17 additions & 21 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@ private void UpdateContexts(ReferencePickerAttribute attribute)
2424

2525
private Type GetParentType(SerializedProperty property, ReferencePickerAttribute attribute)
2626
{
27-
property.GetFieldInfo(out Type propertyType);
27+
var fieldInfo = property.GetFieldInfo(out _);
28+
var fieldType = property.GetProperType(fieldInfo);
2829
var candidateType = attribute.ParentType;
2930
if (candidateType != null)
3031
{
31-
if (propertyType.IsAssignableFrom(candidateType))
32+
if (fieldType.IsAssignableFrom(candidateType))
3233
{
3334
return candidateType;
3435
}
3536

3637
ToolboxEditorLog.AttributeUsageWarning(attribute, property,
37-
$"Provided {nameof(attribute.ParentType)} ({candidateType}) cannot be used because it's not assignable from: '{propertyType}'");
38+
$"Provided {nameof(attribute.ParentType)} ({candidateType}) cannot be used because it's not assignable from: '{fieldType}'");
3839
}
3940

40-
return propertyType;
41+
return fieldType;
4142
}
4243

4344
private void CreateTypeProperty(Rect position, SerializedProperty property, Type parentType)

Assets/Editor Toolbox/Editor/Utilities/PropertyUtility.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace Toolbox.Editor
1010
{
1111
public static partial class PropertyUtility
1212
{
13-
//TODO: temp
13+
//NOTE: last non-reflection implementation was ok but support for [SerializeReference] makes it a bit slow
14+
// unfortunately UnityEditor.ScriptAttributeUtility.GetFieldInfoFromProperty is internal so we have to retrive it using reflection
1415
private static readonly MethodInfo getGetFieldInfoFromPropertyMethod =
1516
ReflectionUtility.GetEditorMethod("UnityEditor.ScriptAttributeUtility", "GetFieldInfoFromProperty",
1617
BindingFlags.NonPublic | BindingFlags.Static);
@@ -254,7 +255,6 @@ public static Type GetScriptTypeFromProperty(SerializedProperty property)
254255
return scriptInstance.GetClass();
255256
}
256257

257-
258258
public static FieldInfo GetFieldInfo(this SerializedProperty property)
259259
{
260260
return GetFieldInfo(property, out _);
@@ -266,16 +266,9 @@ public static FieldInfo GetFieldInfo(this SerializedProperty property, out Type
266266
var result = getGetFieldInfoFromPropertyMethod.Invoke(null, parameters) as FieldInfo;
267267
propertyType = parameters[1] as Type;
268268
return result;
269-
270-
//NOTE: ...
271-
//return GetFieldInfoFromProperty(property, out propertyType);
272-
}
273-
274-
public static FieldInfo GetFieldInfo(this SerializedProperty property, out Type propertyType, Object target)
275-
{
276-
return GetFieldInfoFromProperty(property, out propertyType, target.GetType());
277269
}
278270

271+
[Obsolete("This method is no longer safe, use GetFieldInfo() instead.")]
279272
public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, out Type type)
280273
{
281274
var classType = GetScriptTypeFromProperty(property);
@@ -288,8 +281,7 @@ public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, ou
288281
return GetFieldInfoFromProperty(property, out type, classType);
289282
}
290283

291-
//NOTE: ...
292-
//TODO: make it internal
284+
[Obsolete("This method is no longer safe, use GetFieldInfo() instead.")]
293285
public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, out Type type, Type host)
294286
{
295287
const BindingFlags fieldFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
@@ -397,7 +389,7 @@ public static SerializedProperty GetSize(this SerializedProperty array)
397389

398390
public static T GetAttribute<T>(SerializedProperty property) where T : Attribute
399391
{
400-
return GetAttribute<T>(property, GetFieldInfoFromProperty(property, out _));
392+
return GetAttribute<T>(property, GetFieldInfo(property, out _));
401393
}
402394

403395
public static T GetAttribute<T>(SerializedProperty property, FieldInfo fieldInfo) where T : Attribute
@@ -407,7 +399,7 @@ public static T GetAttribute<T>(SerializedProperty property, FieldInfo fieldInfo
407399

408400
public static T[] GetAttributes<T>(SerializedProperty property) where T : Attribute
409401
{
410-
return GetAttributes<T>(property, GetFieldInfoFromProperty(property, out _));
402+
return GetAttributes<T>(property, GetFieldInfo(property, out _));
411403
}
412404

413405
public static T[] GetAttributes<T>(SerializedProperty property, FieldInfo fieldInfo) where T : Attribute

Assets/Editor Toolbox/Tests/Editor/PropertyUtilitesTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void TestGetParentPass(string propertyPath, string parentName)
3636
public void TestGetValuePass(string propertyPath, object value)
3737
{
3838
var property = scriptableObject.FindProperty(propertyPath);
39-
var fieldInfo = property.GetFieldInfo(out _, scriptableObject.targetObject);
39+
var fieldInfo = property.GetFieldInfo(out _);
4040
property.SetProperValue(fieldInfo, value);
4141
var newValue = property.GetProperValue(fieldInfo);
4242
Assert.AreEqual(value, newValue);

Assets/Examples/Scenes/SampleScene.unity

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,15 +583,16 @@ MonoBehaviour:
583583
id: 1
584584
var3:
585585
id: 2
586-
vars: []
586+
vars:
587+
- id: 3
587588
references:
588589
version: 1
589590
00000000:
590591
type: {class: SampleBehaviour6/ClassWithInterface1, ns: , asm: Assembly-CSharp}
591592
data:
592593
go: {fileID: 977748987}
593594
var1:
594-
id: 3
595+
id: 4
595596
00000001:
596597
type: {class: SampleBehaviour6/ClassWithInterface3, ns: , asm: Assembly-CSharp}
597598
data:
@@ -602,6 +603,8 @@ MonoBehaviour:
602603
var1: 0
603604
mat: {fileID: 2100000, guid: 7404c70251f9d0045a4aabaa49d83963, type: 2}
604605
00000003:
606+
type: {class: , ns: , asm: }
607+
00000004:
605608
type: {class: SampleBehaviour6/Struct, ns: , asm: Assembly-CSharp}
606609
data:
607610
var1: 1

0 commit comments

Comments
 (0)