주녘공부일지

[Unity] 모바일 터치 감지 방법 본문

GameEngine/UnityEngine - Class

[Unity] 모바일 터치 감지 방법

주녘 2022. 2. 16. 20:39
728x90

1. MonoBehaviour.OnMouseXX 함수

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MobileTest : MonoBehaviour
{
    void OnMouseUp()
    {
        // 커서가 올라가면 호출 (모바일에선 터치 시에 호출)
    }
    
    void OnMouseDrag()
    {
       // 커서가 눌린 상태에서 드래그 시 호출
    }

    void OnMouseDown()
    {
        // 커서가 눌릴 때 호출    
    }
}

https://docs.unity3d.com/kr/530/ScriptReference/MonoBehaviour.html

 

Unity - 스크립팅 API: MonoBehaviour

Javascript를 사용하는 경우에, 자동으로 MonoBehaviour를 상속받습니다. C# 또는 Boo언어를 사용하는 경우에는 명시적으로 MonoBehaviour를 상속받아야 합니다. Note: MonoBehaviour를 비활성화 하는 체크박스(에

docs.unity3d.com

 

2. EventSystems - IPointerXX, IDragHandler

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class MobileTest : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
    public void OnDrag(PointerEventData eventData)
    {
        // 커서가 눌린 상태에서 이동 시 호출
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        // 커서가 눌릴 때 호출
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        // 커서를 누른 상태에서 떼면 호출
    }
}

using UnityEngine.EventSystems; 선언해야 사용 가능

Scene의 UI - EventSystems 오브젝트가 존재해야 함

 

https://docs.unity3d.com/kr/530/ScriptReference/EventSystems.PointerEventData.html

 

Unity - 스크립팅 API: PointerEventData

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. 닫기

docs.unity3d.com

 

3. Input 클래스 - GetMouseButtonXX

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MobileTest : MonoBehaviour
{
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            // 커서가 눌릴 때 호출
        }
        if(Input.GetMouseButtonUp(0))
        {
            // 커서가 눌린 상태에서 떼었을 때 호출
        }
    }
}

스크립트를 담은 오브젝트가 활성화되어 있다면, 마우스의 클릭 위치에 관계없이 호출됨

 

https://docs.unity3d.com/kr/530/ScriptReference/Input.html

 

Unity - 스크립팅 API: Input

Input Manager에 설정된 각 축을 읽고 모바일 장치의 멀티터치/가속도계 데이터에 접근을 하는 경우에 이 클래스를 사용합니다. 다음의 기본 설정 축과 Input.GetAxis 함수를 이용해서 해당 축을 읽습니

docs.unity3d.com

 

4. 구조체 Touch 활용 ( namespace UnityEngine - public struct Touch )

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MobileTest : MonoBehaviour
{
    void Update()
    {
        if(Input.touchCount > 0) // 터치 갯수
        {
            Touch touch = Input.GetTouch(0); // 0번 - 가장 먼저 터치된 정보
            switch (touch.phase)
            {
                case TouchPhase.Began:
                    // 터치가 발생한 시점
                    break;
                case TouchPhase.Moved:
                    // 터치가 움직일 때
                    break;
                case TouchPhase.Stationary:
                    // 터치가 대기중일 때
                    break;
                case TouchPhase.Ended:
                    // 터치가 끝났을 때
                    break;
                case TouchPhase.Canceled:
                    // 5개 이상의 터치가 발생하여 터치가 취소됐을 때
                    break;
            }
        }
    }
}

 

touch.position 은 Screen 포지션 값

 

https://docs.unity3d.com/kr/530/ScriptReference/Touch.html

 

Unity - 스크립팅 API: Touch

Devices can track a number of different pieces of data about a touch on a touchscreen, including its phase (ie, whether it has just started, ended or moved), its position and whether the touch was a single contact or several taps. Furthermore, the continui

docs.unity3d.com

728x90

'GameEngine > UnityEngine - Class' 카테고리의 다른 글

[Unity3D] Vector3  (0) 2022.02.21
[Unity3D] position (위치) // 플레이어 이동 예제  (0) 2022.02.21
[Unity] ForceMode(ForceMode2D)  (0) 2022.01.26
[Unity] Prefab이란? (+ 불러오기)  (0) 2022.01.25
[Unity] BoxCollider2D  (0) 2022.01.18