Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question I can't check display property of uielement

Discussion in 'UI Toolkit' started by ggppgg, Feb 3, 2023.

  1. ggppgg

    ggppgg

    Joined:
    Sep 27, 2021
    Posts:
    3
    I want to check display property. in uibuilder editor, I set the display property None, but uielement.style.display is Flex in script. So, i have to set all uielements' display None when i cache it. I think it is inconvenient. do you have any other solution?
    Any input would be greatly appreciated. Thank you
     
  2. noirb

    noirb

    Joined:
    Apr 10, 2014
    Posts:
    84
    In code, check
     element.resolvedStyle
    instead of
     element.style
     
  3. razamgar

    razamgar

    Joined:
    Apr 7, 2020
    Posts:
    14
    resolvedStyle
    indeed provides the expected value, but I would like to know why does it do so while
    style
    does not? What's the difference? Official Unity documentation on the
    style
    property specifies that it, and I quote,

    To me it seems like should return values parsed from what I defined in UI Builder, but it does not.

    It'd be amazing if someone like @uDamian or @arichards chimed in to help clear this matter.
     
    Last edited: May 11, 2023
  4. noirb

    noirb

    Joined:
    Apr 10, 2014
    Posts:
    84
    So, in general you should write to
    element.style
    (if you need to set any style values in code) and read from
    element.resolvedStyle
    .

    As for why, this is more of a guess than an explanation but bear with me, style values on an element must be computed from a variety of sources. You may have multiple classes applied to the element, some of these classes may specify values which are relative to the element's parent, an element may inherit styles from its ancestors in the hierarchy, and it may have inline styles applied to it in UXML or code. These values may also change from frame to frame for a variety of reasons. So, even if
    element.style
    exactly matched the values that you see when you select that element in the UI builder, those values may not be the actual values that the element is using for rendering.
    element.resolvedStyle
    essentially contains the final calculated values accounting for all sources on the current frame.

    Let's take
    width
    for example. In the easiest case, you may have an inline style applied which specifies the width in pixels. In this case you may expect
    style
    and
    resolvedStyle
    to both contain the same value. However, on the frame that you add that element to the hierarchy while
    style.width
    may have the exact value you expect (because you set it just now),
    resolvedStyle.width
    may be
    NaN
    because that element hasn't been processed as part of any layout update yet so its true width hasn't yet been computed. On the next frame, though,
    resolvedStyle.width
    should be updated. And that's the easy case where you have specified a fixed inline style. If instead the element's width was set in a class to
    50%
    , the actual computed width depends not only on the width of the parent, but also other properties of the parent like border-width and padding, so even if checking
    style.width
    returned 50% or
    0.5f * parent.style.width
    , that value could easily be different from the actual rendered width of the element, which you could get from
    resolvedStyle.width
    . And it gets a little more complicated again when you consider that transitions (either applied directly to the element or to one of its ancestors) can also modify this value.
     
    razamgar and oscarAbraham like this.
  5. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    733
    @noirb is right and our documentation for VisualElement.style is incorrect in that this will not contain values specified from USS, only the inline styles of the element.
     
    razamgar likes this.
  6. razamgar

    razamgar

    Joined:
    Apr 7, 2020
    Posts:
    14
    Thank you @noirb, your explanation made it as close to crystal clear to me as it gets.

    Also, thanks @antoine-unity for confirming! Are there any plans for updating documentation having that in mind? I'm fairly sure others will appreciate less ambiguity too when trying to understand how UI Toolkit works.
     
  7. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    733
    We will fix the API documentation and I will suggest adding an How To guide to help clarify the topic of retrieving the effective styles of a visual element.
     
    razamgar likes this.