Last active
November 27, 2018 18:10
-
-
Save batmask/227232cb9af5047b3ae66e3f70c79d54 to your computer and use it in GitHub Desktop.
Detecting touch swipe in unity C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class SwipeManager : MonoBehaviour { | |
| private bool DEBUG = true; | |
| public interface ISwipe{ | |
| void SwipeLeft(); | |
| void SwipeRight(); | |
| void SwipeUp(); | |
| void SwipeDown(); | |
| } | |
| [SerializeField] | |
| //StartScreenScene startScreenScript; | |
| private Swipe swipeImp; | |
| private class TouchInfo{ | |
| public Vector2 beginPosition; | |
| public Vector2 prevPosition; | |
| public float beginTime; | |
| public TouchInfo(Vector2 bp, Vector2 pp, float bt){ | |
| beginPosition = new Vector2(bp.x, bp.y); | |
| prevPosition = new Vector2(pp.x, pp.y); | |
| beginTime = bt; | |
| } | |
| } | |
| private float maxSwipeTime = 0.5f; | |
| private float minSwipeDistance = 50.0f; | |
| //private Dictionary<int, Vector2> prevPositionDict = new Dictionary<int, Vector2>(10); | |
| private Dictionary<int, TouchInfo> prevPositionDict = new Dictionary<int, TouchInfo>(10); | |
| // Use this for initialization | |
| void Start () { | |
| } | |
| // Update is called once per frame | |
| void Update () { | |
| HandleDragSwipe(); | |
| } | |
| private void HandleDragSwipe(){ | |
| float deltaX = 0; | |
| float deltaY = 0; | |
| // handle touch event | |
| if(Input.touchCount > 0){ | |
| foreach(Touch touch in Input.touches){ | |
| switch(touch.phase){ | |
| case TouchPhase.Began: | |
| if(prevPositionDict.ContainsKey(touch.fingerId)){ | |
| prevPositionDict.Remove(touch.fingerId); | |
| } | |
| prevPositionDict.Add(touch.fingerId, new TouchInfo(touch.position, touch.position, Time.time)); | |
| break; | |
| case TouchPhase.Moved: | |
| // get previous position | |
| Vector2 prevPos = prevPositionDict[touch.fingerId].prevPosition; | |
| // cacluate delta | |
| deltaX = touch.position.x - prevPos.x; | |
| deltaY = touch.position.y - prevPos.y; | |
| // handle delta | |
| // update previous position | |
| prevPositionDict[touch.fingerId].prevPosition.Set(touch.position.x, touch.position.y); | |
| break; | |
| case TouchPhase.Ended: | |
| // check is swipe | |
| float dragDistanceX = touch.position.x - prevPositionDict[touch.fingerId].beginPosition.x; | |
| float dragDistanceY = touch.position.y - prevPositionDict[touch.fingerId].beginPosition.y; | |
| float deltaTime = Time.time - prevPositionDict[touch.fingerId].beginTime; | |
| if(dragDistanceX >= dragDistanceY){ | |
| if(Mathf.Abs(dragDistanceX) > minSwipeDistance && deltaTime < maxSwipeTime){ | |
| // This is swipe! | |
| if(dragDistanceX > 0){ | |
| //to the left | |
| DebugHelper.Log(DEBUG, "[HandleDragSwipe] to the LEFT SWIPE!"); | |
| swipeImp.SwipeLeft(); | |
| }else if(dragDistanceX < 0){ | |
| //to the right | |
| DebugHelper.Log(DEBUG, "[HandleDragSwipe] to the RIGHT SWIPE!"); | |
| swipeImp.SwipeRight(); | |
| } | |
| } | |
| }else{ | |
| if(Mathf.Abs(dragDistanceY) > minSwipeDistance && deltaTime < maxSwipeTime){ | |
| // This is swipe! | |
| if(dragDistanceY > 0){ | |
| //to the up | |
| DebugHelper.Log(DEBUG, "[HandleDragSwipe] to the UP SWIPE!"); | |
| swipeImp.SwipeUp(); | |
| }else if(dragDistanceY < 0){ | |
| //to the down | |
| DebugHelper.Log(DEBUG, "[HandleDragSwipe] to the right SWIPE!"); | |
| swipeImp.SwipeDown(); | |
| } | |
| } | |
| } | |
| // remove from dictionary | |
| prevPositionDict.Remove(touch.fingerId); | |
| break; | |
| } | |
| } | |
| } | |
| // Handle mouse event | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment