- Home /
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:
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 evaluatedThe
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.
Ooo I remember having a problem like this - I have a library of stuff that can find the parents of elements because of it I think. I don't have that code to hand, but I seem to remember getting "data" - which as you quite rightly say isn't the "Property Array" it's the sub element that the serialization system uses to hold the data.
The principle of the library stuff I wrote was to walk the property path towards the property you have from the serialized object root and then use the thing 2 levels above,
Thanks for mentioning that, I took the serializedObject
from the property, and searched for the real name, and it seems I got it (it makes no sense to me):
var realProp = property.serializedObject.FindProperty("waypoints"); // could this be it?
bool isArray = realProp.isArray; // true
But unfortunately this eli$$anonymous$$ates the whole purpose of my property drawer idea - what's the point if I can't get the name right at runtime? - One thing I could do is, pass the name via the attribute:
[AwesomeCollection("waypoints")]
public Transform[] waypoints;
But that's just silly... what happens if I refactor and rename? I'd rather not think about it...
If you happen to remember how you got to the array, I'd be interested.
Thanks :)
Answer by Jamora · Dec 27, 2013 at 03:01 PM
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.
So the targetObject of the Serialized property given to us in the OnGui Event is the whole Behaviour class? not the individual item? and the propertyPath is the individual item? I don't think that makes sense, but does that mean we are intended to be able to easily affect a property drawer with other content from the same behaviour?
Your answer
Follow this Question
Related Questions
Getting PropertyAttribute from PropertyDrawer.OnGUI 0 Answers
Display Custom Inspectors for each class in a List<> 1 Answer
Custom Editor/Custom Property Drawer combo, breaks Enable button 1 Answer
How do PropertyAttribute instances work in an array of structs? 1 Answer
Get SerializedProperty from outside class for use in popup (PropertyDrawer) 1 Answer