본문 바로가기
개발/C#

유니티 C# 화면 터치 드래그 Touch Drag 간단 사용법

by SPNK 2022. 7. 6.
반응형
  • 코드 작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchManager : MonoBehaviour
{
    private Vector2 startPos;

    public float minSwipeDistY = 50f;
    public float minSwipeDistX = 50f;

    private bool firstSwipe = false;

    public string direction = ""; //Up, Down, Left, Right로 입력이 들어옵니다.

    void Update()
    {
        if (Time.timeScale == 0) { InputButtonUp(); return; }
    }

    public void InputButtonUp()
    {
        firstSwipe = false;
    }

    public void InputButtonDown()
    {
        startPos = Input.mousePosition;
        firstSwipe = true;
    }

    public void InputButtonStay()
    {
        if (!firstSwipe) return;
        float swipeDistHorizontal = Mathf.Abs(Input.mousePosition.x - startPos.x);
        float swipeDistVertical = Mathf.Abs(Input.mousePosition.y - startPos.y);
        if (swipeDistHorizontal > swipeDistVertical)
        {
            if (swipeDistHorizontal > minSwipeDistX)
            {
                float swipeValue = Input.mousePosition.x - startPos.x;
                if (swipeValue > 0)
                {
                    direction = "Right";

                    firstSwipe = false;
                }
                else if (swipeValue < 0)
                {
                    direction = "Left";

                    firstSwipe = false;
                }
            }
        }
        else
        {
            if (swipeDistVertical > minSwipeDistY)
            {
                float swipeValue = Input.mousePosition.y - startPos.y;
                if (swipeValue > 0)
                {
                    direction = "Up";

                    firstSwipe = false;
                }
                else if (swipeValue < 0)
                {
                    direction = "Down";

                    firstSwipe = false;
                }
            }
        }

        Invoke("OnReset", 0.25f);
    }

    private void OnReset()
    {
        direction = "";
    }
}
반응형

댓글