visualElement.style.translate.value is always (0, 0) for elemets that were set in UiBuilder?

I have an element with the following style settings (screenshot below). It is clearly not in the 0 Length by x.

However, if I call _visualElement.style.translate.value.x.value at runtime, it will return 0. The element itself is shown correctly, in the middle of the screen.

And if I set x value manually using something like:

Length x = _visualElement.style.translate.value.x;
Length y = _visualElement.style.translate.value.y;
_visualElement.style.translate = new StyleTranslate(new Translate(x, y));

this will move the element to the left border of the screen (where the 0 is)

So, in a word, it returns the wrong value, but if you don’t touch it, the element is displayed correctly (in the middle of the screen).

If you touch it, it will be redrawn at the 0 by x.

The question is, is that a bug, or am I missing something?

The same thing happens for elements moved with a Position style using UIBuilder or for Relative (not Absolute) elements.

Unity version 2021.3.24f1

Try using resolvedStyle instead of style. This will contain the value from the UXML.

style - the inputs for the computed styles
resolvedStyle - the “outputs” of the layout pass and the values that are ultimately used.

3 Likes

This one returns the correct valuers. Thank you a lot! So, technically, I have to read it from resolvedStyle and then write it to style, when I move the element according to the player input, right?

1 Like