参考 https://blog.csdn.net/fdyshlk/article/details/78768078
public sealed class RectTransformUtility { // 计算child在root中的区域位置 public static Bounds CalculateRelativeRectTransformBounds(Transform root, Transform child); // 计算trans在直接父容器中的区域位置 public static Bounds CalculateRelativeRectTransformBounds(Transform trans); /// <summary> // 交换rect的Width和Height值以及Pivot的X和Y值 // recursive=true, 子对象的值也会发生交换 // recursive=false, 只交换自己的值 // keepPositioning=true, 保持rect的PosX和PosY不变,元素位置可变. // keepPositioning=false, 保持元素位置不变,rect的PosX和PosY可变. /// </summary> public static void FlipLayoutAxes(RectTransform rect, bool keepPositioning, bool recursive); /// <summary> // axis=0, 翻转Pivot的X值 (例如 X=0.4, 翻转后变为X=0.6) // axis=1, 翻转Pivot的Y值 /// </summary> public static void FlipLayoutOnAxis(RectTransform rect, int axis, bool keepPositioning, bool recursive); // 使point对齐像素坐标 public static Vector2 PixelAdjustPoint(Vector2 point, Transform elementTransform, Canvas canvas); // 使rectTransform的四个角对齐像素坐标 public static Rect PixelAdjustRect(RectTransform rectTransform, Canvas canvas); // 判断screenPoint是否在rect区域内 (Render Mode=Screen Space - Overlay) public static bool RectangleContainsScreenPoint(RectTransform rect, Vector2 screenPoint); // 判断screenPoint是否在rect区域内 (cam为UI像机) public static bool RectangleContainsScreenPoint(RectTransform rect, Vector2 screenPoint, Camera cam); // 屏幕坐标转rect本地坐标 public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint); // 从cam发出一条射线穿过screenPos点 public static Ray ScreenPointToRay(Camera cam, Vector2 screenPos); // 屏幕坐标转世界坐标(相对于rect),可理解为从screenPoint发出的射线与rect的交点。 public static bool ScreenPointToWorldPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector3 worldPoint); // 世界坐标转屏幕坐标 public static Vector2 WorldToScreenPoint(Camera cam, Vector3 worldPoint); }