🤔 Unity, huh, how?

🤔

UI Toolkit: Resolved style

UI Toolkit resolves styles and layout asynchronously.
resolvedStyle, layout, contentRect, paddingRect, and others, will return invalid values (typically NaN) before it has been initialised.

Resolution

These members are initialised after an element is created in the hierarchy.
You can use GeometryChangedEvent or AttachToPanelEvent to read from these values when they are valid.

_visualElement.RegisterCallback<GeometryChangedEvent>(
    _ =>
    {
        // _visualElement.resolvedStyle will contain initialised values.
    }
);