1. 이벤트 처리
Input 클래스는 사용자로부터 입력받은 정보를 포함하고 있다. Input 클래스의 정보를 기반으로 캐릭터를 움직이거나, 게임 오브젝트를 선택하는 로직을 구현한다. 사용자로부터 입력받은 정보, 터치 및 마우스 클릭 이벤트를 처리할 때에 주의할 점이 있다. 월드 상의 게임 오브젝트를 선택할 때, 캔버스(Canvas) 상의 버튼(Button), 패널(Panel) 등의 UI 요소와 겹쳐서 이벤트가 동시에 처리될 수 있다는 점이다.
UI(User Interface)는 게임에 필수적인 구성요소이며, 사용자의 입력을 처리하는 역할을 담당한다. UI를 구성하는 요소들은 대부분 사용자와 상호작용하도록 구현되어 있다. 데스크탑 컴퓨터에서 동작하는 게임을 제작할 때에는 UI의 노출을 최대한 배제하고, 사용자가 버튼을 입력하도록 구성을 할 수도 있다. 그러나 이러한 방식에는 한계가 있다.
일반적으로 클릭, 터치 이벤트가 발생한 위치가 UI 오브젝트라면 UI 오브젝트에서 이벤트를 처리한다. 그러므로 UI 오브젝트에서 이벤트를 처리하고, Input 클래스를 사용하여 이벤트를 처리하지 않도록 로직을 구현한다.
2. EventSystem
UI의 이벤트 처리는 EventSystem에서 담당한다. EventSystem은 마우스 입력, 스크린 터치 입력, 키보드 입력과 같은 이벤트를 오브젝트에 전송한다. Canvas를 생성하면, 자동으로 EventSystem이 등록되는 것을 확인할 수 있다. 직접 등록도 가능하다. 위치는 아래와 같다.
GameObject → UI → EventSystem
EventSystem을 사용하기 위해서는 다음을 추가해야한다.
using UnityEngine.EventSystems;
2.1 IsPointerOverGameObject()
EventSystem의 IsPointerOverGameObject()는 마우스 위치가 UI 오브젝트에 있을 때 true 값을 반환한다. 그렇지 않은 경우는 false 값을 반환한다.
bool isOverGameObject = EventSystem.current.IsPointerOverGameObject();
bool isOverGameObject = EventSystem.current.IsPointerOverGameObject(int pointerId);
함수의 전달 인자(Argument)를 사용해 특정 입력 장치로부터 발생한 이벤트인지를 확인하고, 처리할 수 있다.
파라미터(Parameter) : int PointerId
- -1 : Left Mouse
- -2 : Right Mouse
- -3 : Center Mouse
- 0 : Sing Tap
- 1 : Double Tap
- 2 : Triple Tap
3. 구현
모바일 입력 방식과 데스크탑 컴퓨터의 입력 방식은 다르다.
3.1 Mouse
데스크탑 컴퓨터의 입력방식은 마우스를 사용한다. 마우스에 대한 처리는 다음을 사용하면 된다.
EventSystem.current.IsPointerOverGameObject()
Sample
using UnityEngine;
using UnityEngine.EventSystems;
public class TestEvent : MonoBehaviour{
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown())
{
if(EventSystem.current.IsPointerOverGameObject())
{
return;
}
// Do something
}
}
}
3.2 Touch
모바일의 입력 방식과 데스크탑 컴퓨터의 입력 방식은 다르다. 모바일의 입력 방식은 터치이다.
터치를 사용할 경우에는 IsPointerOverGameObject()에 fingerId를 전달 인자로 사용한다.
EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId);
using UnityEngine;
using UnityEngine.EventSystems;
public class TestEvent : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if(Input.touchCount > 0)
{
if(EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
return;
}
// Do something.
}
}
}
Reference
[1] EventSystems.
https://docs.unity3d.com/kr/2020.2/Manual/EventSystem.html
이벤트 시스템 - Unity 매뉴얼
이벤트 시스템은 키보드, 마우스, 터치, 커스텀 입력 등 입력 기반 애플리케이션의 오브젝트에 이벤트를 전송하는 방법입니다. 이벤트 시스템은 이벤트를 전송에 함께 작용하는 일부 컴포넌트
docs.unity3d.com
[2] IsPointerOverGameObject()
EventSystems.EventSystem-IsPointerOverGameObject - Unity 스크립팅 API
Is the pointer with the given ID over an EventSystem object?
docs.unity3d.com
[2] PointerId
Unity - Scripting API: EventSystems.PointerEventData.pointerId
When using a mouse the pointerId returns -1, -2, or -3. These are the left, right and center mouse buttons respectively. When using touchscreen on mobile versions (such as iPad, iPhone, or Android), touches go from 0 up to however many touches the device s
docs.unity3d.com
'유니티 > 기초' 카테고리의 다른 글
유니티 이미지 파일 렌더링 (Image File Rendering) (0) | 2025.02.11 |
---|---|
유니티 코드 디버깅 (Debugging) (0) | 2021.01.16 |
유니티 Invoke (0) | 2020.12.01 |
유니티 코루틴(Coroutine) 과 IEnumerator (0) | 2020.11.30 |
유니티 데이터 저장 및 불러오기 (PlayerPrefs) (0) | 2020.11.29 |