Touch Event 2D / 3D 코드 처리
터치 이벤트는 Update에서 사용하는 것을 권장한다.
Update에서는 프레임당 한번을 실행하고 FixedUpdate에서는 물리 tick당 실행되서 프레임마다 차이가 발생이 되기때문에 입력에 대한 손실이 발생가능하다.
관련된 내용 : [링크]
Check for User Input in FixedUpdate?
Hi everyone! I'm new around these here parts! I've been doing some searches on the forums and I've read at least a couple of times that we should...
forum.unity.com
유니티에서는 Touch 입력을 Editor와 Input를 플랫폼에 구분해서 이벤트를 받아야 에디터, 스마트폰디바이스에서 정상동작을 확인할수 있다.
반응형
플랫폼 의존 컴파일 : [링크]
유니티 - 매뉴얼: 플랫폼 의존 컴파일
자동 메모리 관리를 이해하기 플랫폼 의존 컴파일 Unity는 “플랫폼 의존 컴파일”이라는 기능이 있습니다. 여기에는 몇 가지 전 처리기 지시문이 포함되어, 스크립트를 ’파티션화’하여 코드
docs.unity3d.com
Update 에서 Touch Position 받는방법
public void Update()
{
#if UNITY_EDITOR
if (Input.GetMouseButton(0))
{
touchPosition = Input.mousePosition;
input = true;
}
#else
if ( Input.touchCount > 0 )
{
touchPosition = Input.GetTouch(0).position;
input=true;
}
#endif
2D Touch (BoxColider 2D)
Vector2 pos = Camera.main.ScreenToWorldPoint(touchPosition);
Ray2D ray = new Ray2D(pos, Vector2.zero);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
if (hit.collider != null)
{
//touch event
}
3D Touch (BoxColider)
RaycastHit hitObj;
Ray ray = Camera.main.ScreenPointToRay(touchPosition);
if(Physics.Raycast(ray, out hitObj, Mathf.Infinity)){
//touch event
}
터치 좌표를 기준으로 레이(Ray)를 쏴서 Coliider가 있는 오브젝트가 Hit 됐는지 판단한다.
★★☆☆☆
반응형
'개발 > Unity' 카테고리의 다른 글
Unity)해결) Request error (error) : UnityEditor.AsyncHTTPClient:Done(State, Int32) (6) | 2020.07.28 |
---|---|
Unity) 프로젝트가 확장될 때 코드 설계 방법 (8) | 2020.07.15 |
Unity) 스크롤 뷰 커스텀 ScrollView Custom (가로, 세로 동시) (13) | 2020.07.01 |
정리)Dev Weeks: 작업 효율을 높이기 위한 유니티 UI 제작 프로그래밍 패턴들 (2) | 2020.06.29 |
Unity) txt 파일 생성 및 저장(I/O) (0) | 2020.06.16 |
댓글