3

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.

0

2 Answers 2

8

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;
0

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.