Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5.  
  6. [RequireComponent(typeof(Image))]
  7. public class DragSwipeHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
  8. {
  9. [Range(0f, 1f)]
  10. public float swipeThreshold = 0.5f;
  11.  
  12. public UnityEvent OnSwipeUp;
  13. public UnityEvent OnSwipeDown;
  14. public UnityEvent OnSwipeRight;
  15. public UnityEvent OnSwipeLeft;
  16.  
  17. public void OnBeginDrag(PointerEventData eventData) { }
  18.  
  19. public void OnDrag(PointerEventData eventData) { }
  20.  
  21. public void OnEndDrag(PointerEventData data)
  22. {
  23. Vector2 dir = (data.position - data.pressPosition).normalized;
  24.  
  25. if (OnSwipeRight != null && dir.x > swipeThreshold)
  26. OnSwipeRight.Invoke();
  27.  
  28. if (OnSwipeLeft != null && dir.x < -swipeThreshold)
  29. OnSwipeLeft.Invoke();
  30.  
  31. if (OnSwipeUp != null && dir.y > swipeThreshold)
  32. OnSwipeUp.Invoke();
  33.  
  34. if (OnSwipeDown != null && dir.y < -swipeThreshold)
  35. OnSwipeDown.Invoke();
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement