GetPropertyHeight And Arrays

Hello,

I have a Issue with Custom Property Drawer, GetPropertyHeight and Arrays. This works fine if the property is used outside of an array or if only one array element is expanded. Having more than one array element
makes the code go out of sync.

I understand that the drawer’s are instanced but my understanding was the properties themselves where per Serialized object.

The example code:

[CustomPropertyDrawer(typeof(MyAttribute))]
public class MyDrawer : PropertyDrawer
{
  public override float GetPropertyHeight(SerializedProperty property,
  GUIContent label)
  {
   String Path = (MyAttribute)attribute.Path;
   if(Path != property.propertyPath)
  {
    Debug.Log(Path + " != " +property.propertyPath )
  }
// Omitted stuff
    return EditorGUI.GetPropertyHeight(property, label, true);
   }

 
  public override void OnGUI(Rect position,
  SerializedProperty property,
  GUIContent label)
  {
   (MyAttribute)attribute.Path = property.propertyPath;
   //omitted code
   }


}

If One element of the array is open I don’t get any debug log

If two elements (or more) are open I get the message

MyArray.Array.data[0].MyVar != MyArray.Array.data[1].MyVar
MyArray.Array.data[1].MyVar != MyArray.Array.data[0].MyVar

Why is this? Is there anyway to get the correct attribute data?

Apparently, Arrays share the Attribute, and OnGUI and GetPropertyHeight gets called multiple times (one for each element of the array)

The solution to this is

[LIST=1]
[*]public override float GetPropertyHeight(SerializedProperty property,
[*]  GUIContent label)
[*]  {
[*]   String Path = (MyAttribute)attribute.Path;
[*]   if(Path != property.propertyPath)
[*]  {
[*]   data = (MyAttribute)attribute.Paths.[property.propertyPath];
[*]    Debug.Log(Path + " != " +property.propertyPath )
[*]  }
[*]// Omitted stuff
[*]    return EditorGUI.GetPropertyHeight(property, label, true);
[*]   }
[*]

[*]

[*]  public override void OnGUI(Rect position,
[*]  SerializedProperty property,
[*]  GUIContent label)
[*]  {
[*]   (MyAttribute)attribute.Paths.[property.propertyPath] = data;
[*]   //omitted code
[*]   }
[*]
[/LIST]

EditorGUI.GetPropertyHeight actually calls GetPropertyHeight on the property drawer, so you should not call it inside that to avoid infinite recursion (maybe in you case blocked by unity).
EDIT: this is not true: see below

However we could help you better if you share mode code omitted, It’s not clear what you are tying to achieve…

Ok I found something very strange…

here they say EditorGUI.GetPropertyHeight would call GetPropertyHeight on the drawer

so in this example we should get infinite recursion

Ok, from this

We found that my advice on infinite recursion was wrong, sorry for the mistake :confused: