Search Unity

GetPropertyHeight And Arrays

Discussion in 'UGUI & TextMesh Pro' started by BinaryCats, Nov 28, 2016.

  1. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    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:

    Code (csharp):
    1.  
    2. [CustomPropertyDrawer(typeof(MyAttribute))]
    3. public class MyDrawer : PropertyDrawer
    4. {
    5.   public override float GetPropertyHeight(SerializedProperty property,
    6.   GUIContent label)
    7.   {
    8.    String Path = (MyAttribute)attribute.Path;
    9.    if(Path != property.propertyPath)
    10.   {
    11.     Debug.Log(Path + " != " +property.propertyPath )
    12.   }
    13. // Omitted stuff
    14.     return EditorGUI.GetPropertyHeight(property, label, true);
    15.    }
    16.  
    17.  
    18.   public override void OnGUI(Rect position,
    19.   SerializedProperty property,
    20.   GUIContent label)
    21.   {
    22.    (MyAttribute)attribute.Path = property.propertyPath;
    23.    //omitted code
    24.    }
    25.  
    26.  
    27. }
    28.  
    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
    Code (csharp):
    1.  
    2. MyArray.Array.data[0].MyVar != MyArray.Array.data[1].MyVar
    3. MyArray.Array.data[1].MyVar != MyArray.Array.data[0].MyVar
    4.  
    Why is this? Is there anyway to get the correct attribute data?
     
  2. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    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
    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]public override float GetPropertyHeight(SerializedProperty property,
    4. [*]  GUIContent label)
    5. [*]  {
    6. [*]   String Path = (MyAttribute)attribute.Path;
    7. [*]   if(Path != property.propertyPath)
    8. [*]  {
    9. [*]   data = (MyAttribute)attribute.Paths.[property.propertyPath];
    10. [*]    Debug.Log(Path + " != " +property.propertyPath )
    11. [*]  }
    12. [*]// Omitted stuff
    13. [*]    return EditorGUI.GetPropertyHeight(property, label, true);
    14. [*]   }
    15. [*]
    16.  
    17. [*]
    18.  
    19. [*]  public override void OnGUI(Rect position,
    20. [*]  SerializedProperty property,
    21. [*]  GUIContent label)
    22. [*]  {
    23. [*]   (MyAttribute)attribute.Paths.[property.propertyPath] = data;
    24. [*]   //omitted code
    25. [*]   }
    26. [*]
    27. [/LIST]
    28.  
     
  3. scanzy

    scanzy

    Joined:
    Mar 14, 2014
    Posts:
    9
    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...
     
    Last edited: Apr 20, 2017
  4. scanzy

    scanzy

    Joined:
    Mar 14, 2014
    Posts:
    9
  5. scanzy

    scanzy

    Joined:
    Mar 14, 2014
    Posts:
    9