Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

percent position and size

Discussion in 'UIElements' started by Devi-User, Dec 2, 2018.

  1. Devi-User


    Apr 30, 2016
    In css, we can specify a percentage as property value. Will there be something like this in uss?
    I understand that we have such things as flex-grow, and this is great, but sometimes it becomes necessary to set percentage coordinates, for example
    position: absolute;
    left: 50%;

    If this is not possible, then the next question is how to find out the current size of the parent container? I noticed that it is not calculated immediately, that is, if I set flex-grow and then request layout.width, I won’t get the current width. This is reasonable in terms of performance, but I would like to understand how it can be obtained when it becomes necessary.
  2. uDamian


    Unity Technologies

    Dec 11, 2017
    Percent will at some point be supported in USS. For now, however, you have to stick to flex.

    As for getting the width of an element from the style at runtime, for 19.1 (latest alpha), you can use VisualElement.resolvedStyle.width (for earlier versions layout.width will work too). To account for the delay in style updates (as they need a frame or two to be computed), you can use the GeometryChangeEvent. Register for that event on the element you're interested in and you'll be notified when the styles/layout have been fully computed and are safe to read.