Unity

[Unity] Touch for Mobile - Part1

CommitGuy 2021. 9. 21. 12:46

Unity 개발 타겟을 모바일로 했을 때는 PC로 개발했을때와 달리 키보드 입력등을 받는 구조가 아니기에 모바일용 컨트롤을 위한 개발을 따로 해야만 한다.

 

관련해서 도움이 될만한 내용을 정리해 보도록 하겠습니다.

 

모바일은 키보드 입력을 Default로 받는 구조가 아니기에 화면 터치를 이용해야 합니다.

 

화면 터치의 단계는 총 5개의 단계가 있습니다.

 

1. Began: 손가락으로 처음 화면을 터치 했을때 나타나는 단계

2. Moved: 손가락이 화면과의 접촉을 끊지 않고 위치를 이동했을때의 단계

3. Stationary: 손가락이 화면과의 접촉을 끊지 않고 아무런 이동없이 한곳에 머무를때의 단계

4. Ended: 손가락이 화면을 더 이상 터치하지 않을 때

5. Cancelled: 손가락의 화면 터치 tracking을 취소할때

 

실제 유니티에서 위의 단계들을 확인하기 위한 코드를 짜서 직접 확인해 보도록 하겠습니다.

 

https://gist.github.com/hankyojeong/800f0ce98d3a73dbefc11974d06a44ba

 

Can see the touchphase in unity

Can see the touchphase in unity. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

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

public class MobileController : MonoBehaviour
{
    public Text phaseDisplayText;

    private Touch theTouch;
    private float timeTouchEnded;
    private float displayTime = .5f;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (Input.touchCount > 0)
        {
            theTouch = Input.GetTouch(0);
            phaseDisplayText.text = theTouch.phase.ToString();

            if (theTouch.phase == TouchPhase.Ended)
            {
                timeTouchEnded = Time.time;
            }
        }
        else if (Time.time - timeTouchEnded > displayTime)
        {
            phaseDisplayText.text = "";
        }
    }
}

 

 

 

'Unity' 카테고리의 다른 글

Unity Design Pattern - Object Pool  (2) 2024.06.03
유니티 3D Model 파괴 효과 만들기  (0) 2024.02.26
Unity Android Plugin 만들기  (2) 2024.01.28
Mac에서의 classes.jar 위치  (0) 2021.10.31
[Unity] Animator Animation 재시작 방법  (0) 2021.10.29