SerializedProperty isn't being detected as an array?

I’m writing a property drawer for lists and arrays.

AwesomeCollectionAttribute.cs

[AttributeUsage(AttributeTargets.Field)]
public class AwesomeCollectionAttribute : PropertyAttribute { }

TestMB.cs

public class TestMB : MonoBehaviour
{
	[AwesomeCollection]
	public Transform[] waypoints;
}

I have a few problems:

1- In the drawer’s OnGUI it’s not detecting the property as an array!

AwesomeCollectionDrawer.cs

[CustomPropertyDrawer(typeof(AwesomeCollectionAttribute))]
public class AwesomeCollectionDrawer : PropertyDrawer
{
	public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
	{
		Debug.Log("OnGUI ran from awesome");
		if (property.isArray)
			Debug.Log("Size: " + property.arraySize);

		GUI.Label(position, property.name, EditorStyles.boldLabel);
	}
}

The log isn’t getting executed. And if I try to get the size immediately without checking if the property is an array, I get:

Retrieving array size but no array was provided
UnityEditor.SerializedProperty:get_arraySize()
AwesomeCollectionDrawer:OnGUI(Rect, SerializedProperty, GUIContent) (at Assets/Editor/AwesomeCollectionDrawer.cs:13)
UnityEditor.DockArea:OnGUI()

Here’s an interesting thing though:

There’s a few strange things here:

  1. The name is “data” where it should be “waypoints”, right?
  2. The property path, is the path of the first element of the array, wtf?
  3. The isArray and arraySize couldn’t be evaluated
  4. The objectReferenceTypeString is Transform, shouldn’t it be Transform[]?

Here’s what I think: All of these things, suggest that I don’t have a hold of the actual array, but what I have is its first element? … or, maybe its base pointer? (because, as we know from C++ the array name is nothing but a const pointer to its first element… but even in that case I could index it) What’s going on here?!

2- The other problem, is that although I’m overriding the OnGUI for my property, I can still see some default stuff (the size, and the fold) I don’t want any default thing to show up - you can also see this “data” thing - another proof that I don’t have a hold of the actual array, but its individual elements:

19796-inspector.png

Any idea what’s going on?

  1. Why isn’t the property picking up the array?
  2. Why am I getting some default stuff drawn for me? even though I overrode the OnGUI, and never did call base.OnGUI!

Thanks a lot.

You can get the runtime name of the variable with the following code:

string[] variableName = property.propertyPath.Split('.');
SerializedProperty p = property.serializedObject.FindProperty(variableName[0]);
Debug.Log(p.isArray); //Prints true

alternatively you could consider only geting a substring from index 0 to the first dot from the propertyPath, but that’s a matter of taste.

You can also use reflection:

fieldInfo.Name

will return the variable name.