Skip to content

Commit ea4512e

Browse files
committed
Possibility to get a valid FieldInfo from [SerializeReference] properties
1 parent 2d61980 commit ea4512e

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ internal static FieldInfo GetFieldInfo(this SerializedProperty property, out Typ
253253

254254
internal static FieldInfo GetFieldInfo(this SerializedProperty property, out Type propertyType, Object target)
255255
{
256-
return GetFieldInfoFromProperty(target.GetType(), property.propertyPath, out propertyType);
256+
return GetFieldInfoFromProperty(property, out propertyType, target.GetType());
257257
}
258258

259259
public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, out Type type)
@@ -265,14 +265,15 @@ public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, ou
265265
return null;
266266
}
267267

268-
return GetFieldInfoFromProperty(classType, property.propertyPath, out type);
268+
return GetFieldInfoFromProperty(property, out type, classType);
269269
}
270270

271-
public static FieldInfo GetFieldInfoFromProperty(Type host, string fieldPath, out Type type)
271+
public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, out Type type, Type host)
272272
{
273273
FieldInfo field = null;
274274
type = host;
275275

276+
var fieldPath = property.propertyPath;
276277
var members = GetPropertyFieldTree(fieldPath, false);
277278
for (var i = 0; i < members.Length; i++)
278279
{
@@ -287,10 +288,21 @@ public static FieldInfo GetFieldInfoFromProperty(Type host, string fieldPath, ou
287288
continue;
288289
}
289290

291+
const BindingFlags fieldFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
290292
FieldInfo foundField = null;
291293
for (var currentType = type; foundField == null && currentType != null; currentType = currentType.BaseType)
292294
{
293-
foundField = currentType.GetField(member, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
295+
foundField = currentType.GetField(member, fieldFlags);
296+
//NOTE: [SerializeReference] detected? If so we need to check dynamically cached type
297+
if (foundField == null)
298+
{
299+
var parent = property.GetParent();
300+
if (parent != null && parent.propertyType == SerializedPropertyType.ManagedReference)
301+
{
302+
TypeUtilities.TryGetTypeFromManagedReferenceFullTypeName(parent.managedReferenceFullTypename, out var parentType);
303+
foundField = parentType.GetField(member, fieldFlags);
304+
}
305+
}
294306
}
295307

296308
if (foundField == null)

Assets/Examples/Scripts/SampleBehaviour6.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@ public abstract class ClassWithInterfaceBase : Interface1 { }
2727
[Serializable]
2828
public class ClassWithInterface1 : ClassWithInterfaceBase
2929
{
30+
[InLineEditor]
3031
public GameObject go;
32+
[SerializeReference, ReferencePicker]
33+
public Interface1 var1;
3134
}
3235

3336
[Serializable]
3437
public class ClassWithInterface2 : ClassWithInterfaceBase
3538
{
3639
[LeftToggle]
3740
public bool var1;
41+
[InLineEditor]
42+
public Material mat;
3843
}
3944

4045
[Serializable]

0 commit comments

Comments
 (0)