본문 바로가기

소프트웨어/C# + Unity

[Unity C#]유니티 카메라 줌인 줌아웃 Pinch Zoom in out


#유니티카메라 #유니티 줌인(unity zoom in) #유니티 줌아웃(unity  zoom out) #유니티 터치 #유니티 터치제어 #유니티 pinch #유니티 멀티터치


안녕하세요 오늘은 유니티의 기본중의 하나인 손가락 터치를 이용한 줌인/줌아웃 코드에 대해서 설명드리도록 하겠습니다.


코드는 아래와 같습니다. 자세한 주석을 달아놓도록 하겟습니다.




여기서는 카메라 2가지 모드인 perspective mode과 orthographic mode를 구별하여 줌인/줌아웃 코드를 설정하였습니다.


카메라의 옵션은 기본적으로 2가지가 있습니다. 2가지에 대해서는 다음시간에 좀더 살펴보도록 하고 기본적으로 안다는 가정하에 설명을 드리도록




using UnityEngine; public class PinchZoom : MonoBehaviour { public float perspectiveZoomSpeed = 0.5f; // perspective mode. public float orthoZoomSpeed = 0.5f; // orthographic mode. void Update() { // 손가락으로 줌인/아웃의 경우 무조건 2손가락이 터치가 되어야 하기 때문에 Count = 2일 경우만 동작 if (Input.touchCount == 2) { // Store both touches. Touch touchZero = Input.GetTouch(0); //첫번째 손가락 좌표 Touch touchOne = Input.GetTouch(1); //두번째 손가락 좌표 // deltaposition은 deltatime과 동일하게 delta만큼 시간동안 움직인 거리를 말한다.

// 현재 position에서 이전 delta값을 빼주면 움직이기 전의 손가락 위치가 된다. Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition; Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition; // 현재와 과거값의 움직임의 크기를 구한다. float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude; float touchDeltaMag = (touchZero.position - touchOne.position).magnitude; // 두 값의 차이는 즉 확대/축소할때 얼만큼 많이 확대/축소가 진행되어야 하는지를 결정한다. float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag; // If the camera is orthographic... if (camera.isOrthoGraphic) { // ... change the orthographic size based on the change in distance between the touches. camera.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed; // Make sure the orthographic size never drops below zero. camera.orthographicSize = Mathf.Max(camera.orthographicSize, 0.1f); } else { // Otherwise change the field of view based on the change in distance between the touches. camera.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed; // Clamp the field of view to make sure it's between 0 and 180. camera.fieldOfView = Mathf.Clamp(camera.fieldOfView, 0.1f, 179.9f); } } } }




이 코드는 유니티 홈페이지에서 예제로 제공되는 코드이며, 한국어로 간단한 주석을 달아놓았다.




더 자세한 설명과 동영상을 보고 싶다면 아래 URL을 클릭하면 된다.


아래 경로에 들어가면 다양한 예제와 구현된 코드 및 동영상을 확인해볼 수 있다.

https://unity3d.com/kr/learn/tutorials/topics/mobile-touch/pinch-zoom?playlist=17138