GetScreenRect
Returns the screen-space axis-aligned bounding box of the given RectTransform.
Read time 1 minuteLast updated 5 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.UIModule
GetScreenRect(RectTransform)
Returns the screen-space axis-aligned bounding box of the given RectTransform.
public static Rect GetScreenRect(this RectTransform rectTransform)
Parameters
The RectTransform whose screen-space bounding box to compute.
Returns
Type | Description |
|---|---|
| Rect | A Rect describing the screen-space bounding box in pixels. Width and height are always non-negative. |
Remarks
The four world-space corners are projected to screen space using Camera.main. For Screen Space - Overlay canvases, the corners are already in screen-pixel coordinates and camera projection is skipped regardless of whether Camera.main is set, to avoid double-projecting them.
Note: This method uses Camera.main for camera projection. For canvases rendered by a specific non-main camera, use RectTransformUtility.GetScreenRect(RectTransform,Camera) and pass the correct camera explicitly.
Additional Resources: RectTransformUtility.GetScreenRect(RectTransform,Camera).
GetScreenRect(RectTransform, Camera)
Returns the screen-space axis-aligned bounding box of the given RectTransform, using the specified camera for projection.
public static Rect GetScreenRect(this RectTransform rectTransform, Camera camera)
Parameters
The RectTransform whose screen-space bounding box to compute.
The camera to use for world-to-screen projection. If null, or if the RectTransform belongs to a Screen Space - Overlay canvas, camera projection is skipped and the world corners are used directly as screen coordinates.
Returns
Type | Description |
|---|---|
| Rect | A Rect describing the screen-space bounding box in pixels. Width and height are always non-negative. |
Remarks
The four world-space corners are projected to screen space using the provided camera. For Screen Space - Overlay canvases, projection is skipped regardless of the camera argument to avoid double-projecting coordinates that are already in screen space.
Use the no-argument overload RectTransformUtility.GetScreenRect(RectTransform) to project using Camera.main.
Additional Resources: RectTransformUtility.GetScreenRect(RectTransform).