using Core;
using UnityEngine;
namespace Utils
{
///
/// Utility methods for screen space and UI coordinate conversions
///
public static class ScreenSpaceUtility
{
///
/// Convert RectTransform to screen space rect with center position.
/// Useful for screenshot capture or screen-based calculations.
///
/// RectTransform to convert
/// Camera used for coordinate conversion (null = Camera.main)
/// If true, clamps the rect to visible screen area
/// If true, returns center position; if false, returns bottom-left position
/// Screen space rect (position is center or bottom-left based on returnCenterPosition)
public static Rect RectTransformToScreenRect(
RectTransform rectTransform,
Camera camera = null,
bool clampToScreenBounds = true,
bool returnCenterPosition = true)
{
if (rectTransform == null)
{
Logging.Error("[ScreenSpaceUtility] RectTransform is null!");
return new Rect(Screen.width / 2f, Screen.height / 2f, 0, 0);
}
if (camera == null) camera = Camera.main;
// Get world corners (0=bottom-left, 1=top-left, 2=top-right, 3=bottom-right)
Vector3[] corners = new Vector3[4];
rectTransform.GetWorldCorners(corners);
// Determine correct camera based on Canvas render mode
Camera canvasCamera = GetCanvasCamera(rectTransform, camera);
// Convert corners to screen space
Vector2 min = RectTransformUtility.WorldToScreenPoint(canvasCamera, corners[0]);
Vector2 max = RectTransformUtility.WorldToScreenPoint(canvasCamera, corners[2]);
Logging.Debug($"[ScreenSpaceUtility] Canvas mode: {rectTransform.GetComponentInParent