유니티에서 터치 처리

유니티 C# 2017. 8. 13. 15:39

먼저 http://prosto.tistory.com/96 블로그에서 참고하였습니다.



private Touch tempTouchs;

private Vector3 touchedPos;


if(Input.touchCount > 0)//터치가 1개 이상이면.

{

for(int i = 0; i < Input.touchCount; i++) 

{

//UI 클릭시 터치 이벤트 발생 방지

if(EventSystem.current.IsPointerOverGameObject(i) == false)

{

tempTouchs = Input.GetTouch(i);


if(tempTouchs.phase == TouchPhase.Began) //해당 터치가 시작되었다면.

{

//터치 좌표값 가져오기

touchedPos = Camera.main.ScreenToWorldPoint(tempTouchs.position);


break; //한 프레임에는 하나만

}

}

}

}




입니다.


터치 상태 종류

Began(터치 시작)

Canceled(터치 취소)

Ended(터치 종료)

Moved(터치 후 움직임)

Stationary(터치 후 대기)





UI 클릭시 터치 이벤트 발생 방지 관련 주의할 점

Raycast Target이 체크가 되어있어야된다.

만약 체크가 풀려있으면 위의 코드는 상관없어진다.