- Home /
How to get screen position of a RectTransform when canvas render mode set to "Screen Space - Overlay"
I can get a camera and do thing like camera.WorldToScreenPoint to get screen position of a RectTransform when setting Canvas' render mode to "Screen Space - Camera" or "World Space". But when I set render mode to "Screen Space - Overlay", there is no camera rendering this canvas, how can I get screen position of a RectTransform?
Answer by splash5 · Nov 22, 2014 at 05:58 AM
I finally found that just use RectTransformUtility.WorldToScreenPoint and pass Camera as null... Or just use that world position if you are using Screen Space - Overlay mode.
Ah, amazing man! Thanks! I just needed to feed the "camera" parameter with the camera in the canvas' Render Camera (which you can access through Canvas.worldCamera)
great! Thank you! "null" works like expected in overlay mode.
Just as an addition, this is how the decompiled version of the script looks like:
public static Vector2 WorldToScreenPoint(Camera cam, Vector3 worldPoint)
{
if ((Object) cam == (Object) null)
return new Vector2(worldPoint.x, worldPoint.y);
return (Vector2) cam.WorldToScreenPoint(worldPoint);
}
They removed the function from the documentation.
Awesome! I was able to make it work using a RectTransform:
centerOfUIElementAsScreenSpace = RectTransformUtility.WorldToScreenPoint(null, theRectTransform.transform.position);
Your answer
![](https://koobas.hobune.stream/wayback/20220613171025im_/https://answers.unity.com/themes/thub/images/avi.jpg)