Advertisement
Guest User

SwipeController

a guest
Jan 12th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SwipeController : MonoBehaviour
  6. {
  7.     bool isDragging, isMobilePlatform;
  8.     Vector2 tapPoint, SwipeDelta;
  9.     float minSwipeDelta = 130;
  10.     float mSwipeStartTime;
  11.     public float deltaTime;
  12.  
  13.     public enum SwipeType
  14.     {
  15.         LEFT,
  16.         RIGHT,
  17.         UP,
  18.         DOWN
  19.     }
  20.  
  21.     public delegate void OnSwipeInput(SwipeType type);
  22.     public static event OnSwipeInput SwipeEvent;
  23.  
  24.     private void Awake()
  25.     {
  26. #if UNITY_EDITOR || UNITY_STANDALONE
  27.         isMobilePlatform = false;
  28. #else
  29.         isMobilePlatform = true;
  30. #endif
  31.     }
  32.  
  33.     private void Update()
  34.     {
  35.         if (!isMobilePlatform)
  36.         {
  37.             if (Input.GetMouseButtonDown(0))
  38.             {
  39.                 isDragging = true;
  40.                 tapPoint = Input.mousePosition;
  41.                 mSwipeStartTime = Time.time;
  42.             }
  43.             else if (Input.GetMouseButtonUp(0))
  44.             {
  45.                 deltaTime = Time.time - mSwipeStartTime;
  46.                 ResetSwipe();
  47.             }
  48.         }
  49.         else
  50.         {
  51.             if (Input.touchCount > 0)
  52.             {
  53.                 if (Input.touches[0].phase == TouchPhase.Began)
  54.                 {
  55.                     isDragging = true;
  56.                     tapPoint = Input.touches[0].position;
  57.                     mSwipeStartTime = Time.time;
  58.                 }
  59.                 else if (Input.touches[0].phase == TouchPhase.Canceled ||
  60.                         Input.touches[0].phase == TouchPhase.Ended)
  61.                 {
  62.                     deltaTime = Time.time - mSwipeStartTime;
  63.                     ResetSwipe();
  64.                 }
  65.             }
  66.         }
  67.         CalculateSwipe();
  68.     }
  69.  
  70.     void CalculateSwipe()
  71.     {
  72.         SwipeDelta = Vector2.zero;
  73.  
  74.         if (isDragging)
  75.         {
  76.             if (!isMobilePlatform && Input.GetMouseButton(0))
  77.                 SwipeDelta = (Vector2)Input.mousePosition - tapPoint;
  78.             else if (Input.touchCount > 0)
  79.                 SwipeDelta = Input.touches[0].position - tapPoint;
  80.         }
  81.  
  82.         if (SwipeDelta.magnitude > minSwipeDelta)
  83.         {
  84.             if (SwipeEvent != null)
  85.             {
  86.                 if (Mathf.Abs(SwipeDelta.x) > Mathf.Abs(SwipeDelta.y))
  87.                     SwipeEvent(SwipeDelta.x < 0 ? SwipeType.LEFT : SwipeType.RIGHT);
  88.                 else
  89.                     SwipeEvent(SwipeDelta.y > 0 ? SwipeType.UP : SwipeType.DOWN);
  90.             }
  91.             ResetSwipe();
  92.         }
  93.     }
  94.  
  95.     void ResetSwipe()
  96.     {
  97.         isDragging = false;
  98.         tapPoint = SwipeDelta = Vector2.zero;
  99.         mSwipeStartTime = deltaTime = 0f;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement