Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get screen position of a UI element?

I'm trying to get the global position of a UI element.

I've tried so many different ways to get the position but none of them seems to work. The problem comes with the anchors, as i'm moving them and not the UI element position itself (for resolution purposes), the position of the UI showing in the inspector is always 0,0,0. I also tried to get the anchoredPosition and also the Corners to make some calculations but it still not working, I always get (0,0,0) or some incoherent numbers.

 Vector3 pointToTravel = Camera.main.WorldToScreenPoint(objectRectTransform.anchoredPosition);

This 'pointToTravel' variable should hold the screen position in pixels of the ui element.

like image 499
Roger Tello Avatar asked Oct 15 '25 05:10

Roger Tello


2 Answers

anchoredPosition is the

position of the pivot of this RectTransform relative to the anchor reference point.


Since RectTransform inherits from Transform you can simply use

Vector3 pointToTravel = Camera.main.WorldToScreenPoint(objectRectTransform.position);

in order to get the absolute world position.

The position you see in the in the Inspector is always the

  • localPosition for Transform - relative to the parent GameObject.

    or

  • anchoredPosition for RectTransform depending on the anchor settings - relative to the anchor reference point.


However in the case your Canvas is a Screen Space - Overlay all the contained UI (or better RectTransforms) already uses the screenspace coordinates in pixels so the WorldToScreenPoint is unnecessary or better said returns incorrect values.

So in that case you could instead simply use

Vector3 pointToTravel = objectRectTransform.position;
like image 94
derHugo Avatar answered Oct 17 '25 19:10

derHugo


In order to get any information you could need about the dimensions and position of a UI Element from its RectTransform, I wrote this little utility function to convert a RectTransform to a Screen Space - Rect(For Canvas mode Screen Space - Camera).

public static Rect RectTransformToScreenSpace(RectTransform transform, Camera cam, bool cutDecimals = false)
{
    var worldCorners = new Vector3[4];
    var screenCorners = new Vector3[4];

    transform.GetWorldCorners(worldCorners);

    for (int i = 0; i < 4; i++)
    {
        screenCorners[i] = cam.WorldToScreenPoint(worldCorners[i]);
        if (cutDecimals)
        {
            screenCorners[i].x = (int)screenCorners[i].x;
            screenCorners[i].y = (int)screenCorners[i].y;
        }
    }

    return new Rect(screenCorners[0].x,
                    screenCorners[0].y,
                    screenCorners[2].x - screenCorners[0].x,
                    screenCorners[2].y - screenCorners[0].y);
}

From this rect you can easily get all the information that the RectTransform does not give you that easily, such as Screen-Space-position and non-anchored size.

like image 28
AbandonedCrypt Avatar answered Oct 17 '25 19:10

AbandonedCrypt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!