Get actual size of a VisualElement considering PanelSettings PanelScaleMode.ScaleWithScreenSize

Hi,

is there a way to get the actual height (and width) of a VisualElement in pixels?

I tried visualElement.worldBound.height, visualElement.resolvedStyle.height and visualElement.layout.height, but they all ignore the scaling that is configured by uiDocument.panelSettings.scaleMode = PanelScaleMode.ScaleWithScreenSize.

uiDocument.panelSettings.scale does not return the actual scaling either.

Every hint is welcome!

Hey! I think there’s nothing exposed to get the resolved scale. You can calculate the scale using the reference resolution. Note that you will need to take in account the match mode and the match parameter.

Thanks for your reply! It’s unfortunate, that the scale needs to be recalculated manually in order to get the actual size of a visual element.

Instead of recalculating the scale depending on the panel settings parameters, I am going the “cheaper” route:

scale = Screen.height / uiDocument.rootVisualElement.resolvedStyle.height

It works for most cases as far as I can tell.

3 Likes

This worked for me as well! Thank you @WongKit!

Having access to a scale property on the UI Document object would be useful when constraining items within the screen using the UI Document coordinate system.

I am making windows draggable in the UI and making sure a window stays within the screen is a big help.

@WongKit It looks like they actually added a property under the panel object:
image

Which matches the same value I was calculating above.

1 Like

God bless you, friend.