using UnityEngine; namespace UVC.Extension { /// /// RectTransform에 대한 확장 메서드를 제공하는 클래스입니다. /// public static class RectTransformEx { /// /// RectTransform의 여백을 설정합니다. 부모를 기준으로 모든 방향의 여백을 설정하여 크기를 조절합니다. /// 앵커는 부모의 전체를 채우도록 설정됩니다(anchorMin=[0,0], anchorMax=[1,1]). /// /// 대상 RectTransform /// 왼쪽 여백 /// 오른쪽 여백 /// 위쪽 여백 /// 아래쪽 여백 /// /// /// // 패널 RectTransform의 여백 설정 /// RectTransform panelRect = panel.GetComponent(); /// panelRect.SetRectMargin(10f, 10f, 10f, 10f); // 모든 방향에 10의 여백 설정 /// /// // 자식 UI 요소의 여백 다르게 설정 /// RectTransform childRect = childObject.GetComponent(); /// childRect.SetRectMargin(5f, 5f, 20f, 5f); // 위쪽에 더 많은 여백 설정 /// /// public static void SetRectMargin(this RectTransform trans, float left, float right, float top, float bottom) { trans.anchorMin = new Vector2(0, 0); trans.anchorMax = new Vector2(1f, 1f); trans.offsetMin = new Vector2(left, bottom); trans.offsetMax = new Vector2(-right, -top); } /// /// RectTransform의 너비를 설정합니다. /// /// 대상 RectTransform /// 설정할 너비 /// /// /// // 버튼의 너비를 200으로 설정 /// RectTransform buttonRect = searchButton.GetComponent(); /// buttonRect.SetWidth(200f); /// /// public static void SetWidth(this RectTransform trans, float width) { trans.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width); } /// /// RectTransform의 높이를 설정합니다. /// /// 대상 RectTransform /// 설정할 높이 /// /// /// // 패널의 높이를 150으로 설정 /// RectTransform panelRect = panel.GetComponent(); /// panelRect.SetHeight(150f); /// /// public static void SetHeight(this RectTransform trans, float height) { trans.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height); } /// /// RectTransform의 크기를 설정합니다. /// /// 대상 RectTransform /// 설정할 크기 (너비, 높이) /// /// /// // 이미지의 크기를 100x100으로 설정 /// RectTransform imageRect = image.GetComponent(); /// imageRect.SetSize(new Vector2(100f, 100f)); /// /// public static void SetSize(this RectTransform trans, Vector2 size) { trans.SetWidth(size.x); trans.SetHeight(size.y); } /// /// RectTransform의 앵커와 피벗을 중앙으로 설정합니다. /// /// 대상 RectTransform /// /// /// // 버튼의 기준점을 중앙으로 설정 /// RectTransform buttonRect = searchButton.GetComponent(); /// buttonRect.SetAnchorsToCenter(); /// buttonRect.anchoredPosition = Vector2.zero; // 중앙에 배치 /// /// public static void SetAnchorsToCenter(this RectTransform trans) { trans.anchorMin = new Vector2(0.5f, 0.5f); trans.anchorMax = new Vector2(0.5f, 0.5f); trans.pivot = new Vector2(0.5f, 0.5f); } /// /// RectTransform의 앵커와 피벗을 왼쪽 상단으로 설정합니다. /// /// 대상 RectTransform /// /// /// // UI 요소의 기준점을 왼쪽 상단으로 설정 /// RectTransform elementRect = element.GetComponent(); /// elementRect.SetAnchorsToTopLeft(); /// elementRect.anchoredPosition = new Vector2(10f, -10f); // 왼쪽 상단에서 오프셋 설정 /// /// public static void SetAnchorsToTopLeft(this RectTransform trans) { trans.anchorMin = new Vector2(0f, 1f); trans.anchorMax = new Vector2(0f, 1f); trans.pivot = new Vector2(0f, 1f); } /// /// RectTransform을 부모의 크기에 맞게 늘립니다 (모든 여백 0). /// /// 대상 RectTransform /// /// /// // 배경 이미지를 부모 패널에 꽉 채우기 /// RectTransform backgroundRect = backgroundImage.GetComponent(); /// backgroundRect.StretchToParentEdges(); /// /// public static void StretchToParentEdges(this RectTransform trans) { trans.SetRectMargin(0f, 0f, 0f, 0f); } /// /// RectTransform의 위치를 부모 RectTransform 내의 정규화된 좌표(0~1)로 설정합니다. /// /// 대상 RectTransform /// 정규화된 위치 (0,0=왼쪽 하단, 1,1=오른쪽 상단) /// /// /// // UI 요소를 부모의 오른쪽 상단 근처에 배치 /// RectTransform elementRect = element.GetComponent(); /// elementRect.SetNormalizedPosition(new Vector2(0.95f, 0.95f)); /// /// public static void SetNormalizedPosition(this RectTransform trans, Vector2 normalizedPosition) { if (trans.parent is RectTransform parent) { trans.anchorMin = trans.anchorMax = new Vector2(0, 0); trans.pivot = new Vector2(0.5f, 0.5f); Rect parentRect = parent.rect; float x = parentRect.x + parentRect.width * normalizedPosition.x; float y = parentRect.y + parentRect.height * normalizedPosition.y; trans.anchoredPosition = new Vector2(x, y); } } /// /// RectTransform의 월드 좌표 기준 사각 영역을 가져옵니다. /// /// 대상 RectTransform /// 월드 좌표계에서의 Rect /// /// /// // 마우스가 UI 요소 위에 있는지 확인 /// RectTransform elementRect = element.GetComponent(); /// Rect worldRect = elementRect.GetWorldRect(); /// Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); /// bool isOverUI = worldRect.Contains(new Vector2(worldPos.x, worldPos.y)); /// /// public static Rect GetWorldRect(this RectTransform trans) { Vector3[] corners = new Vector3[4]; trans.GetWorldCorners(corners); Vector2 min = corners[0]; Vector2 max = corners[2]; return new Rect(min, max - min); } } }