유니티

RectTransformUntility에 대해서

JunHDev 2025. 1. 7. 17:37

현재 UI를 이용해 디펜스 프로젝트를 진행중이다.
프로젝트를 진행 중 중간에 렌더러 모드를 카메라 모드로 변경하게 되면서 모든 좌표가 기존과 상이하게 되었는데 
이때 알게된 클래스가 RectTransformUtility이다.



RectTransformUntilty란?

UI 좌표 변환 및 충돌 감지 기능을 제공하는 유틸리티 클래스다.

주로 스크린 좌표와 UI 좌표 간 변환, UI 내부 클릭 감지, UI 오브젝트 충돌 체크 등에 사용된다.

 

https://docs.unity3d.com/6000.0/Documentation/ScriptReference/RectTransformUtility.html

 

 

출처 : GPT 

 

🛠 주요 기능:
✅ ScreenPointToLocalPointInRectangle() → 스크린 좌표를 UI localPosition으로 변환
✅ WorldToScreenPoint() → 월드 좌표를 스크린 좌표(픽셀)로 변환
✅ ScreenPointToWorldPointInRectangle() → 스크린 좌표를 월드 좌표로 변환
✅ RectangleContainsScreenPoint() → 특정 UI(RectTransform)가 스크린 좌표를 포함하는지 확인

 


✅ 1.2 WorldToScreenPoint()

📌 월드 좌표를 스크린 좌표로 변환하는 함수

UI에서 3D 오브젝트의 위치를 가져올 때 사용.

💡 사용 예제

Vector3 worldPosition = monster.transform.position; // 3D 오브젝트의 월드 위치 Vector3 screenPosition = RectTransformUtility.WorldToScreenPoint(myCamera, worldPosition);
Debug.Log($"Monster's Screen Position: {screenPosition}");

📌 🔹 결과:
monster.transform.position (월드 좌표) → screenPosition (스크린 픽셀 좌표) 변환됨.

UI 요소를 3D 오브젝트의 위치에 맞게 생성할 때 유용.


✅ 1.3 ScreenPointToWorldPointInRectangle()

📌 스크린 좌표를 특정 RectTransform의 월드 좌표로 변환

주로 UI를 3D 월드 공간에 배치할 때 사용.

💡 사용 예제

 
Vector3 screenPoint = Input.mousePosition; // 마우스 클릭 위치 Vector3 worldPoint;
bool isValid = RectTransformUtility.ScreenPointToWorldPointInRectangle( myUIElement, // 변환할 기준이 되는 UI screenPoint, // 변환할 스크린 좌표 myCamera, // UI를 렌더링하는 카메라 out worldPoint // 변환된 월드 좌표 );
if (isValid) { Debug.Log($"Converted World Position: {worldPoint}"); }

📌 🔹 결과:
스크린 좌표(Input.mousePosition)가 **UI 기준의 월드 좌표(worldPoint)**로 변환됨.

UI 버튼을 클릭하면 3D 오브젝트가 해당 위치에 생성되도록 할 때 유용.


✅ 1.4 RectangleContainsScreenPoint()

📌 특정 RectTransform이 스크린 좌표를 포함하는지 확인하는 함수

UI 클릭 판정 등에 사용.

💡 사용 예제

csharp
코드 복사
Vector2 mouseScreenPos = Input.mousePosition; // 현재 마우스 스크린 좌표 if (RectTransformUtility.RectangleContainsScreenPoint(myUIElement, mouseScreenPos, myCamera)) { Debug.Log("UI 내부에서 클릭되었습니다!"); }

📌 🔹 결과:
✅ 특정 UI 요소 내부를 클릭했는지 확인 가능.
✅ UI 버튼, 패널 클릭 감지 등에 사용.


🎯 2. RectTransformUtility를 활용한 UI 좌표 변환 예제

📌 목표: Screen Space - Camera에서 3D 오브젝트(몬스터)의 위치를 Canvas 내부 UI로 변환

 
// 1️⃣ 3D 몬스터의 월드 좌표를 가져옴 Vector3 monsterWorldPos = monster.transform.position;
// 2️⃣ 몬스터의 월드 좌표를 스크린 좌표로 변환
Vector3 monsterScreenPos = RectTransformUtility.WorldToScreenPoint(myCamera, monsterWorldPos);
// 3️⃣ 스크린 좌표를 Canvas 내부 UI 좌표로 변환
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle( canvasUI, // UI 패널 (Canvas 내부) monsterScreenPos, // 변환할 스크린 좌표 myCamera, // UI를 렌더링하는 카메라 out localPoint // 변환된 localPosition );
// 4️⃣ 변환된 좌표를 UI 요소에 적용 uiMarker.localPosition = localPoint;

이제 몬스터 위치에 따라 UI 마커가 정확한 위치에 표시됨! 🚀🔥


🎯 3. Screen Space - Overlay, Screen Space - Camera, World Space 모드별 사용법

🛠 기능Screen Space - OverlayScreen Space - CameraWorld Space

ScreenPointToLocalPointInRectangle() cam 파라미터 null 가능 cam 필수 cam 필수
WorldToScreenPoint() 불필요 🟢 사용 가능 🟢 사용 가능
ScreenPointToWorldPointInRectangle() 불필요 🟢 사용 가능 🟢 사용 가능
RectangleContainsScreenPoint() cam 없이 가능 cam 필수 cam 필수

📌 💡 Screen Space - Overlay에서는 cam 없이도 동작하지만, Screen Space - Camera에서는 반드시 카메라를 지정해야 합니다!


🚀 최종 정리

✅ RectTransformUtility는 스크린 좌표와 UI 좌표를 변환하는 기능을 제공
✅ Screen Space - Camera 모드에서는 WorldToScreenPoint() → ScreenPointToLocalPointInRectangle() 조합이 필수
✅ Screen Space - Overlay 모드에서는 카메라 없이도 ScreenPointToLocalPointInRectangle() 사용 가능
✅ RectangleContainsScreenPoint()로 UI 클릭 감지 가능