본문 바로가기

유니티3D/자유게시판

유니티 unity 모바일 터치

1. 터치된 손가락 갯수 인식하기

   // 손가락 터치가 1개 일때

   if(Input.touchCount == 1){   ...   }

 

  // 손가락 터치가 2개 이상일때

   if(Input.touchCount >= 2){   ...   }

 

   // 손가락 터치가 오직 1개일때만

   if(Input.touchCount < 2){   ...  }

 

2. 터치의 단계는 5가지

   // 터치가 시작되었을 때

   if(Input.GetTouch(0).phase == TouchPhase.Began){  ... }

 

   // 터치된 손가락이 움직일 때

   if(Input.GetTouch(0).phase == TouchPhase.Moved){ ... }

 

   // 터치된 손가락이 그자리에 가만히 있을 때

   if(Input.GetTouch(0).phase == TouchPhase.Stationary){ ... }

 

   // 터치된 손가락이 스크린에서 떨어질 때

   if(Input.GetTouch(0).phase == TouchPhase.Ended){ ... }

 

   // 모바일폰을 귀에 갖다 대거나 touch tracking을 수행하지 않아야 할 경우에

   if(Input.GetTouch(0).phase == TouchPhase.Canceled){ ... }

 

3. 터치의 응용

   // 손가락 두 개가 움직일 때

   if(Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved){ ... }

 

   // 손가락 하나는 가만히 있고 다른 하나는 움직일 때

   if(Input.GetTouch(0).phase == TouchPhase.Stationary && Input.GetTouch(1).phase == TouchPhase.Moved){ ... }

 

   // 손가락 한 개가 스크린을 움직일 때 속도

   if(Input.GetTouch(0).phase == TouchPhase.Moved){

      float speedX = Input.GetTouch(0).deltaPosition.x / Input.GetTouch(0).deltaTime;   // 횡방향 속도

      float speedY = Input.GetTouch(0).deltaPosition.y / Input.GetTouch(0).deltaTime;   // 종방향 속도

   }

 

   // 스크린 찍은 위치에서 Raycast 할 때

   if(Input.GetTouch(0).phase == TouchPhase.Began){

      Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);

      RaycastHit hit;

      if(Physics.Raycast(ray, out hit, 10.0f)){

         Instantiate(something, hit.point, Quaternion.identity);

      }

   }


출처 - http://blog.daum.net/zevie/21