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