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:
- The name is “data” where it should be “waypoints”, right?
- The property path, is the path of the first element of the array, wtf?
- The
isArray
andarraySize
couldn’t be evaluated - The
objectReferenceTypeString
isTransform
, shouldn’t it beTransform[]
?
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:
Any idea what’s going on?
- Why isn’t the property picking up the array?
- 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.