250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- Hp바
- Object Pooling
- 일시정지
- 유니티
- Parallax
- Unity
- Vector3
- Blend Type
- Animation State Machine
- raycast
- Object Poling
- 2D슈팅게임
- rotation
- apk
- 플레이어 이동
- Scrooling
- CSharp #자료구조
- 프리팹
- joystick
- 오브젝트 풀링
- 패럴렉스
- rigidbody
- Ainimation Blending
- 플레이어 방향전환
- 스크롤링
- Transform
- Prefabs
- Hpbar
- raycasting
- LayerMark
Archives
- Today
- Total
주녘공부일지
[Unity] 모바일 터치 감지 방법 본문
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
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
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
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 포지션 값
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 |