Unity

RectTransformUtility.ScreenPointToLocalPointInRectangle

CommitGuy 2025. 2. 11. 23:29
RectTransformUtility.ScreenPointToLocalPointInRectangle

 

ScreenPoint(스크린 좌표)를 RectTransform 좌표계의 Local Point로 변환시켜주는 함수이다.

 

public static bool ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint);

파라미터를 살펴보면 

첫번째 파라미터는 변환 기준 좌표계의 Canvas RectTransform

두번째 파라미터는 출력하고자 하는 스크린 좌표

세번째 파라미터는 스크린좌표와 연관된 카메라

네번째 파라미터는 변환된 좌표를 저장할 변수 이다

 

예시를 잠깐 보면

public void OnDrag(PointerEventData eventData)
{
    RectTransformUtility.ScreenPointToLocalPointInRectangle(containerRect, eventData.position, eventData.pressEventCamera, out Vector2 position);
}

Drag시 containerRect의 RectTransform을 기준 좌표계로 터치한 지점(eventData.position)을 RectTransform 기준 좌표계로 변환시킨후 그 변환시킨 데이터를 position 변수에 넣어준다

 

반응형