• RectTransformUtility
    • 源码

    RectTransformUtility

    源码

    1. using scm = System.ComponentModel;
    2. using uei = UnityEngine.Internal;
    3. using RequiredByNativeCodeAttribute = UnityEngine.Scripting.RequiredByNativeCodeAttribute;
    4. using UsedByNativeCodeAttribute = UnityEngine.Scripting.UsedByNativeCodeAttribute;
    5. using System;
    6. using UnityEngine;
    7. using Object = UnityEngine.Object;
    8. namespace UnityEngine
    9. {
    10. public sealed partial class RectTransformUtility
    11. {
    12. private static Vector3[] s_Corners = new Vector3[4];
    13. private RectTransformUtility() {}
    14. public static bool RectangleContainsScreenPoint(RectTransform rect, Vector2 screenPoint)
    15. {
    16. return RectangleContainsScreenPoint(rect, screenPoint, null);
    17. }
    18. public static bool ScreenPointToWorldPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector3 worldPoint)
    19. {
    20. worldPoint = Vector2.zero;
    21. Ray ray = ScreenPointToRay(cam, screenPoint);
    22. var plane = new Plane(rect.rotation * Vector3.back, rect.position);
    23. float dist;
    24. if (!plane.Raycast(ray, out dist))
    25. return false;
    26. worldPoint = ray.GetPoint(dist);
    27. return true;
    28. }
    29. public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint)
    30. {
    31. localPoint = Vector2.zero;
    32. Vector3 worldPoint;
    33. if (ScreenPointToWorldPointInRectangle(rect, screenPoint, cam, out worldPoint))
    34. {
    35. localPoint = rect.InverseTransformPoint(worldPoint);
    36. return true;
    37. }
    38. return false;
    39. }
    40. public static Ray ScreenPointToRay(Camera cam, Vector2 screenPos)
    41. {
    42. if (cam != null)
    43. return cam.ScreenPointToRay(screenPos);
    44. Vector3 pos = screenPos;
    45. pos.z -= 100f;
    46. return new Ray(pos, Vector3.forward);
    47. }
    48. public static Vector2 WorldToScreenPoint(Camera cam, Vector3 worldPoint)
    49. {
    50. if (cam == null)
    51. return new Vector2(worldPoint.x, worldPoint.y);
    52. return cam.WorldToScreenPoint(worldPoint);
    53. }
    54. public static Bounds CalculateRelativeRectTransformBounds(Transform root, Transform child)
    55. {
    56. RectTransform[] rects = child.GetComponentsInChildren<RectTransform>(false);
    57. if (rects.Length > 0)
    58. {
    59. Vector3 vMin = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
    60. Vector3 vMax = new Vector3(float.MinValue, float.MinValue, float.MinValue);
    61. Matrix4x4 toLocal = root.worldToLocalMatrix;
    62. for (int i = 0, imax = rects.Length; i < imax; i++)
    63. {
    64. rects[i].GetWorldCorners(s_Corners);
    65. for (int j = 0; j < 4; j++)
    66. {
    67. Vector3 v = toLocal.MultiplyPoint3x4(s_Corners[j]);
    68. vMin = Vector3.Min(v, vMin);
    69. vMax = Vector3.Max(v, vMax);
    70. }
    71. }
    72. Bounds b = new Bounds(vMin, Vector3.zero);
    73. b.Encapsulate(vMax);
    74. return b;
    75. }
    76. return new Bounds(Vector3.zero, Vector3.zero);
    77. }
    78. public static Bounds CalculateRelativeRectTransformBounds(Transform trans)
    79. {
    80. return CalculateRelativeRectTransformBounds(trans, trans);
    81. }
    82. public static void FlipLayoutOnAxis(RectTransform rect, int axis, bool keepPositioning, bool recursive)
    83. {
    84. if (rect == null)
    85. return;
    86. if (recursive)
    87. {
    88. for (int i = 0; i < rect.childCount; i++)
    89. {
    90. RectTransform childRect = rect.GetChild(i) as RectTransform;
    91. if (childRect != null)
    92. FlipLayoutOnAxis(childRect, axis, false, true);
    93. }
    94. }
    95. Vector2 pivot = rect.pivot;
    96. pivot[axis] = 1.0f - pivot[axis];
    97. rect.pivot = pivot;
    98. if (keepPositioning)
    99. return;
    100. Vector2 anchoredPosition = rect.anchoredPosition;
    101. anchoredPosition[axis] = -anchoredPosition[axis];
    102. rect.anchoredPosition = anchoredPosition;
    103. Vector2 anchorMin = rect.anchorMin;
    104. Vector2 anchorMax = rect.anchorMax;
    105. float temp = anchorMin[axis];
    106. anchorMin[axis] = 1 - anchorMax[axis];
    107. anchorMax[axis] = 1 - temp;
    108. rect.anchorMin = anchorMin;
    109. rect.anchorMax = anchorMax;
    110. }
    111. public static void FlipLayoutAxes(RectTransform rect, bool keepPositioning, bool recursive)
    112. {
    113. if (rect == null)
    114. return;
    115. if (recursive)
    116. {
    117. for (int i = 0; i < rect.childCount; i++)
    118. {
    119. RectTransform childRect = rect.GetChild(i) as RectTransform;
    120. if (childRect != null)
    121. FlipLayoutAxes(childRect, false, true);
    122. }
    123. }
    124. rect.pivot = GetTransposed(rect.pivot);
    125. rect.sizeDelta = GetTransposed(rect.sizeDelta);
    126. if (keepPositioning)
    127. return;
    128. rect.anchoredPosition = GetTransposed(rect.anchoredPosition);
    129. rect.anchorMin = GetTransposed(rect.anchorMin);
    130. rect.anchorMax = GetTransposed(rect.anchorMax);
    131. }
    132. private static Vector2 GetTransposed(Vector2 input)
    133. {
    134. return new Vector2(input.y, input.x);
    135. }
    136. }
    137. }

    ?