[Unity] ui 이벤트가 일반 input 이벤트 구분하는 방법 (UI 버튼 뒤의 게임오브젝트나 터치를 막아야 할 경우)
▣ Unity 게임 만들기/▷ 메모 / 2016. 11. 29. 17:22
유니티에서 UI객체를 클릭 혹은 터치를 했을 때 게임 중 입력 이벤트를 처리하는 부분과 같이 처리되는 문제가 발생하는 경우가 많은데요~
이런경우엔,
using UnityEngine.EventSystems;
...
if (EventSystem.current.IsPointerOverGameObject(pointId)) return;
EventSystem.current.IsPointerOverGameObject(pointId)
함수는 포인터가 중간에 UI객체를 만나면 true 를 뱉어내는 함수입니다.
pointId : 터치 또는 마우스 ID 입니다
요걸로 게임중 입력이벤트 이전에 컷트해버리는 식으로 처리하면 됩니다.
예제코딩
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | void Update () { if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) { if (Input.touchCount > 0) { for (int i = 0; i < Input.touchCount; i++) { if (EventSystem.current.IsPointerOverGameObject(i)) return; } } print ("터치!!!"); } else { if (Input.GetMouseButtonDown (0)) { if (EventSystem.current.IsPointerOverGameObject()) return; print ("클릭!!!"); } } } | cs |
위 코딩으로 테스트 해보시면 모바일 터치환경과 에디터환경에서 테스트 차이를 확인가능합니다.