- Home /
Convert local point in RectTransform to screen space
I need to convert a point in a RectTransform to screen space. Basically, I need to do the opposite of what RectTransformUtility.ScreenPointToLocalPointInRectangle does. I have looked at Transform.TransformPoint, except that I need points converted to screen space, not world space.
As an example, the point (0.5, 0.5) in the local space of a RectTransform should get converted to the screen space coordinates of the center of the RectTransform.
How can I accomplish this?
It is probably something like this:
Vector2 pos = rect.position + new Vector2 (0.5, 0.5);
Camera.main.WorldToScreenPoint (pos);
Answer by Tomer-Barkan · Apr 19, 2016 at 03:59 PM
use GetWorldCorners() or Transform.TransformPoint() and then use RectTransformUtility.WorldToScreenPoint().
Vector3[] corners = new Vector3[4];
rect.GetWorldCorners(corners);
Debug.Log("Screen point1: " + new Vector3(rect.rect.xMax, rect.rect.yMin, 0) + rect.position);
foreach (Vector3 corner in corners) {
Debug.Log("World point: " + corner);
Debug.Log("Screen point: " + RectTransformUtility.WorldToScreenPoint(null, corner));
Debug.Log("Viewport: " + Camera.main.ScreenToViewportPoint(corner));
}
Answer by nisovin · Jul 22, 2015 at 09:54 PM
You should be able to use Transform.TransformPoint to convert from local space to world space, then use Camera.WorldToScreenPoint to convert to the screen point of the given camera.
Could you provide a code snippet please? I have tried doing something like this before, but it didn't seem to work.
It doesn't work. the world coordinates it returns is relative to the canvas not the camera, so the second transformation will return a really large number.
I'm still trying to figure this one out myself.
I got it by just using RectTransform.TransformPoint. Correct me if I'm wrong but I believe this converts to screen space. At least in my testing this worked fine.
Your answer
![](https://koobas.hobune.stream/wayback/20220611232548im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Convert RectTransform rect to screen space 12 Answers
Get position of specific letter in UI Text 4 Answers
Spawn Prefab at Particle Collision 0 Answers
How do i move an object relative to another objects axis. 2 Answers
How to change the Top and Bottom (rect.yMin and yMax) properties of a rectTransform, in a script? 2 Answers