Search Unity

Bug Adding a VisualElement child to a Label breaks the Label's worldbounds and flex behavior

Discussion in 'UI Toolkit' started by mikejm_, Jul 3, 2022.

  1. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
    In UI Toolkit, if you add a child VisualElement to a Label, it breaks the Label's worldbounds and flex performance. Instead of the worldbounds representing the Label, they will be representative of the child that was added to it. The Label will no longer flex or layout properly.

    This is demonstrated in a simple bug report project I made (IN-911).

    1) When there is no child attached to the red Label (public bool on "UIDocument" set false) it lays out fine. The red background of the Label fills the Label space and the bounds are calculated correctly.

    childoflabel1.png

    2) When a child is added to the red Label (simple cyan square) it breaks the Label's layout. The red background of the Label no longer fills the Label space and now conforms to the cyan square child.

    childoflabel2.png

    This can be worked around by avoiding adding children to Labels, but sometimes it is desirable to position a visual element relative to a label (eg. a text cursor or caret or box around a word) and having to the child instead it to a different element requires extra calculations to offset it correctly.

    It would be helpful if we could position children directly relative to a Label as the Label's children without breaking the Label. Thanks.

    I am not sure if this is already known, but I found this problem once before, and now lost 3-4 hours today again before figuring out it was the problem again. I presume this is not intended behavior and although it is not crippling (I can work around it), it might be nice to have it fixed, and perhaps you are not already aware.

    Thanks.
     
  2. sebastiend-unity

    sebastiend-unity

    Unity Technologies

    Joined:
    Nov 9, 2015
    Posts:
    184
    Thanks for pointing this out. I will add a bug report -- for the record, I am only getting this wrong behaviour when the Label's white-space property is set to "normal".
     
  3. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
    Thanks. I actually submitted a bug report already if it helps: IN-911.