Camera.WorldToScreenPoint + free aspect woes

Hi all,
I’m trying to place some label elements over top of some game objects. This works fine when my Panel Settings Scale Mode is set to a Constant - but we want it set to “Scale With Screen Size”
This seems to mean that the coordinates returned from

Camera.WorldToScreenPoint(pTransform);

Are not accurate on screen unless I am in a set resolution. See video:
https://drive.google.com/file/d/1H2FgCDg-9QFKN_qcjoOdWYP9Kcjs9cAX/view?usp=sharing
(Sorry about the low quality, but you still see the required info)

I did find this thread with a solution (Issue with WorldToScreenPoint - Questions & Answers - Unity Discussions)
However this solution doesn’t work in UI Toolkit because there is no canvas.
I’ve also checked my UIDocument.PanelSettings.scale and it’s always 1.

Does anyone know the math, or how I can find out?

Thanks

EDIT: I forgot to post my PanelSettings:

Here is a similar post, maybe it can help: converting world to screenspace coordinates offset issue

1 Like

Thanks.
I ended up creating a helper method that I hope others will find useful:

    public static Vector2 GetScreenPositionFromTransform(Camera pCamera, Vector3 pTransformPosition, VisualElement pRoot)
    {
        var worldToScreenPoint = pCamera.WorldToScreenPoint(pTransformPosition);
        return RuntimePanelUtils.ScreenToPanel(pRoot.panel, new Vector2(worldToScreenPoint.x, Screen.height - worldToScreenPoint.y));
    }

It’s working wonderfully -
I think the only thing it assumes is that your root visual element is of course the entirety of the screen size, and then the ui elements that you are positioning are of course being positioned relative to the same entirety.