1.3.0: Added SerializeReferenceTo as a helper for assigning managed type to fields with SerializeReference

This commit is contained in:
Anders Ejlersen 2021-12-05 13:19:42 +01:00
parent ea849a715d
commit e1d0e0e90b
8 changed files with 216 additions and 8 deletions

View file

@ -421,12 +421,18 @@ namespace Module.Inspector.Editor
foreach (string path in property.propertyPath.Split('.'))
{
type = obj.GetType();
FieldInfo field = type.GetField(path);
FieldInfo field = obj.GetType().GetField(path);
if (field == null)
continue;
obj = field.GetValue(obj);
type = field.FieldType;
}
if (type != null && type.IsArray)
type = type.GetElementType();
return type;
}
@ -500,14 +506,14 @@ namespace Module.Inspector.Editor
return "Unable to draw value as string";
}
public static int IndexOfProperty(this SerializedProperty sp, SerializedProperty element)
public static int IndexOfProperty(this SerializedProperty property, SerializedProperty element)
{
if (!sp.isArray)
if (!property.isArray)
return -1;
for (var i = 0; i < sp.arraySize; i++)
for (var i = 0; i < property.arraySize; i++)
{
SerializedProperty e = sp.GetArrayElementAtIndex(i);
SerializedProperty e = property.GetArrayElementAtIndex(i);
if (e.propertyPath.Equals(element.propertyPath))
return i;
@ -515,5 +521,21 @@ namespace Module.Inspector.Editor
return -1;
}
public static FieldInfo GetFieldInfo(this SerializedProperty property)
{
object obj = property.serializedObject.targetObject;
FieldInfo field = null;
foreach (string path in property.propertyPath.Split('.'))
{
field = obj.GetType().GetField(path);
if (field != null)
obj = field.GetValue(obj);
}
return field;
}
}
}