samedi 4 juin 2022

Type.GetField on array field returns null

I'm trying to get an array field from class using Reflections. On a simple field it works, on Array it doesn't.

This is the class

public abstract class Condition : ScriptableObject
{
    public string Name;
    public virtual bool IsVerified() { return false; }
}

public class ExampleScript : MonoBehaviour
{
    [SerializeField] Condition _condition = null;
    [SerializeField] Condition[] _conditions = new Condition[0];
}

[CustomPropertyDrawer(typeof(Condition))]
public class ConditionPropertyDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginProperty(position, label, property);

        Type propertyType = GetPropertyType(property);

        EditorGUI.EndProperty();
    }

    private Type GetPropertyType(SerializedProperty property)
    {
        Type parentType = property.serializedObject.targetObject.GetType();
        Debug.Log($"{parentType} => {property.propertyPath}");
        FieldInfo fi = parentType.GetField(property.propertyPath, BindingFlags.NonPublic | BindingFlags.Instance);
        Debug.Log(fi);
        return fi.FieldType;
    }
}

This is where I'm getting Fields:

Type parentType = property.serializedObject.targetObject.GetType();
Debug.Log($"{parentType} => {property.propertyPath}");
FieldInfo fi = parentType.GetField(property.propertyPath, BindingFlags.NonPublic | BindingFlags.Instance);
Debug.Log(fi);

The Debug prints (Condition var):

ExampleScript => _condition
MyFullNameSpace.Condition _condition

The Debug prints (Condition[] var):

ExampleScript => _conditions.Array.data[0]
Null

Why it doesn't return right FieldInfo?
Thanks in advance





Aucun commentaire:

Enregistrer un commentaire