Last active
April 5, 2020 20:33
-
-
Save RoeiRubach/d0391c4256ffb2cac910034e0d3320d3 to your computer and use it in GitHub Desktop.
[Mobile Finger Swiping Detector] #SwipeDetector
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; | |
| using UnityEngine; | |
| public struct SwipeData | |
| { | |
| public Vector2 StartPosition, EndPosition; | |
| public SwipeDirection Direction; | |
| } | |
| public enum SwipeDirection | |
| { | |
| Up, | |
| Down, | |
| Left, | |
| Right | |
| } | |
| public class SwipeDetector : MonoBehaviour | |
| { | |
| // This script handles only 1 finger detection. | |
| private Vector2 _fingerDownPosition, _fingerUpPosition; | |
| [Tooltip("Enables continuous finger movement detection on the screen")] | |
| [SerializeField] private bool _isMovementDetectionEnable = false; | |
| [Tooltip("The minimum distance for a swipe")] | |
| [Range(35, 150)] | |
| [SerializeField] private int _swipeDeadzone = 35; | |
| public static event Action<SwipeData> OnSwipe = delegate { }; | |
| private void Update() | |
| { | |
| if (Input.touchCount != 0) | |
| { | |
| Touch touch = Input.GetTouch(0); | |
| if (touch.phase == TouchPhase.Began) | |
| { | |
| _fingerDownPosition = touch.position; | |
| _fingerUpPosition = touch.position; | |
| } | |
| // To enable the "Moved" phase, turn the boolean to true via the inspector. | |
| if (_isMovementDetectionEnable && touch.phase == TouchPhase.Moved) | |
| { | |
| _fingerDownPosition = touch.position; | |
| DetectSwipe(); | |
| } | |
| if (touch.phase == TouchPhase.Ended) | |
| { | |
| _fingerDownPosition = touch.position; | |
| DetectSwipe(); | |
| } | |
| } | |
| } | |
| private void DetectSwipe() | |
| { | |
| if (SwipeDeadzoneCheck()) | |
| { | |
| if (IsVerticalSwipe()) | |
| { | |
| var direction = _fingerDownPosition.y - _fingerUpPosition.y > 0 ? SwipeDirection.Up : SwipeDirection.Down; | |
| SendSwipe(direction); | |
| } | |
| else | |
| { | |
| var direction = _fingerDownPosition.x - _fingerUpPosition.x > 0 ? SwipeDirection.Right : SwipeDirection.Left; | |
| SendSwipe(direction); | |
| } | |
| _fingerUpPosition = _fingerDownPosition; | |
| } | |
| } | |
| private bool SwipeDeadzoneCheck() => VerticalMovementDistance() > _swipeDeadzone || HorizontalMovementDistance() > _swipeDeadzone; | |
| private bool IsVerticalSwipe() => VerticalMovementDistance() > HorizontalMovementDistance(); | |
| private float VerticalMovementDistance() => Mathf.Abs(_fingerDownPosition.y - _fingerUpPosition.y); | |
| private float HorizontalMovementDistance() => Mathf.Abs(_fingerDownPosition.x - _fingerUpPosition.x); | |
| private void SendSwipe(SwipeDirection direction) | |
| { | |
| SwipeData swipeData = new SwipeData() | |
| { | |
| Direction = direction, | |
| StartPosition = _fingerDownPosition, | |
| EndPosition = _fingerUpPosition | |
| }; | |
| OnSwipe(swipeData); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment