Unity 화면의 크기와 3D 좌표 매핑
💡 모바일에서 스크린 크기가 다양하기 때문에 3d 좌표계의 boundary를 찾는 방법이 필요해서 구하는 방법이다.
화면의 크기에 따른 3D 좌표 구하기Permalink
🔔 Raycast를 이용하여 구했으며 (0, 0) 위치와 (width, height) 위치의 좌표를 구하였다.
1. 코드Permalink
public Vector3 leftBottomBoundary, rightTopBoundary;
private void initScreenBoundary()
{
Camera camera = Camera.main;
RaycastHit hit;
int layerMask = (1 << GameConstants.LAYER_GROUND);
if (Physics.Raycast(camera.ScreenPointToRay(new Vector3(0, 0, 0)), out hit, Mathf.Infinity, layerMask))
{
leftBottomBoundary = hit.point;
Debug.Log("leftBottomBoundary : " + leftBottomBoundary);
}
if (Physics.Raycast(camera.ScreenPointToRay(new Vector3(camera.pixelWidth, camera.pixelHeight, 0)), out hit, Mathf.Infinity, layerMask))
{
rightTopBoundary = hit.point;
Debug.Log("rightTopBoundary : " + rightTopBoundary);
}
}
2. 코드 설명Permalink
-
Screen의 x,y 좌표는 Camera.pixelWith, pixelHeight 를 이용해서 구함
-
Ray를 지형과 만나는 곳만 체크하기 위해 layermask를 사용.
-
(0, 0), (w, h) 에 해당하는 3d point를 변수에 저장