Search Unity

Question Label-Element size is not calculated when style.display is set from None to Flex

Discussion in 'UI Toolkit' started by manuelgoellnitz, Nov 18, 2020.

  1. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    I have a simple Button with Label Elements as children.
    The text can be quite long so the label element grows and resizes the whole button.
    upload_2020-11-18_16-48-14.png

    When hide the button with display = DisplayStyle.None and let it reappear with display = DisplayStyle.Flex the Label's size is not calculated (until you hover with the mouse over the button) and looks like this:

    upload_2020-11-18_16-53-43.png

    This is form a small test project, with nothing else. So there are no side effects.

    This is the sample code I used for hiding the button:
    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.Space))
    2.         {
    3.             if (isHidden)
    4.             {
    5.                 root.style.display = DisplayStyle.Flex;
    6.             }
    7.             else
    8.             {
    9.                 root.style.display = DisplayStyle.None;
    10.             }
    11.  
    12.             isHidden = !isHidden;
    13.         }
    Is this a bug of the UIToolkit or did I just used it the wrong way?
    is there a better way to hide and show UI Elements?
     
  2. uBenoitA

    uBenoitA

    Unity Technologies

    Joined:
    Apr 15, 2020
    Posts:
    220
    It looks like a bug indeed.

    Setting style.display = DisplayStyle.None is the recommended way to go; it shouldn't lead to label size calculations being skipped once the element is displayed again. I've tried to reproduce your bug but at first glance I'm not able to replicate it.

    What version of Unity are you using? And what version of the UI Toolkit package?
     
  3. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    I use Unity 2020.1.13f1 and UI toolkit preview12
     
  4. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    Since you said that this behaviour looks like a bug i Created a bug report where I attached my test project: Case 1293761

    Ah one thing: You said you couldn't reproduce the bug. It does not happen when you add linebreaks in your text yourself. Only when the line breaks occur from "white-space: normal"
     
  5. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    700
    Are you looking at this in Play Mode or Edit Mode? The Game View is only updated in certain cases when in Edit Mode, whereas in Play Mode you get updates constantly (because that's the whole point of running the game :D )

    EDIT: never mind, I read the case and you clearly say to hit play, sorry! I have just been working on getting the Game View to update properly so that rang a bell here.
     
  6. uBenoitA

    uBenoitA

    Unity Technologies

    Joined:
    Apr 15, 2020
    Posts:
    220
    > Ah one thing: You said you couldn't reproduce the bug. It does not happen when you add linebreaks in your text yourself. Only when the line breaks occur from "white-space: normal"

    You're right. I'm now able to reproduce. Good catch! Could you submit a bug report, please?
     
  7. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    I did 2 posts ago :)
     
  8. visose

    visose

    Joined:
    Nov 2, 2014
    Posts:
    28
    Hi, this bug seems to still be present. When text breaks due to "white-space: normal", the parent's size doesn't update.
    Is there a work around for this?
     
  9. magnetic_scho

    magnetic_scho

    Joined:
    Feb 2, 2020
    Posts:
    97
    Last edited: Mar 18, 2021
  10. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    700
    UI Toolkit package version 1.0.0-preview.15 is not yet but it will be soon, thank you for your patience!