Created
October 12, 2016 02:52
-
-
Save crappygraphix/79ab95992a3877f52e4d7a58ab1688b1 to your computer and use it in GitHub Desktop.
Naive way to track swipes and taps. Just attach to a UI Element.
This file contains 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 UnityEngine; | |
using System.Collections; | |
using UnityEngine.EventSystems; | |
public enum MovementAction{ | |
None, | |
Tap, | |
SwipeLeft, | |
SwipeRight, | |
SwipeUp, | |
SwipeDown | |
} | |
public class ListenToInputPanel : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerUpHandler, IPointerDownHandler, IPointerClickHandler { | |
static MovementAction movementAction = MovementAction.None; | |
public static MovementAction ConsumeAction() { | |
var ret = movementAction; | |
movementAction = MovementAction.None; | |
return ret; | |
} | |
public static MovementAction PeekAction() { | |
return movementAction; | |
} | |
static bool dragging; | |
public static bool Dragging{ | |
get{ | |
return dragging; | |
} | |
} | |
Vector2 delta; | |
public void OnBeginDrag (PointerEventData pointerEventData) { | |
delta = pointerEventData.position; | |
} | |
public void OnDrag(PointerEventData pointerEventData){ | |
// Without me OnBeginDrag and OnEndDrag doesn't get called. Why not? Who knows? Not me. | |
} | |
public void OnEndDrag(PointerEventData pointerEventData) { | |
delta = pointerEventData.position - delta; | |
delta.Normalize (); | |
if (delta.x < 0 && Mathf.Abs (delta.y) < 0.25f) { | |
movementAction = MovementAction.SwipeLeft; | |
return; | |
} | |
if (delta.x > 0 && Mathf.Abs (delta.y) < 0.25f) { | |
movementAction = MovementAction.SwipeRight; | |
return; | |
} | |
if (delta.y < 0) { | |
movementAction = MovementAction.SwipeDown; | |
return; | |
} | |
if (delta.y > 0) { | |
movementAction = MovementAction.SwipeUp; | |
return; | |
} | |
} | |
public void OnPointerClick(PointerEventData pointerEventData){ | |
movementAction = MovementAction.Tap; | |
} | |
public void OnPointerUp(PointerEventData pointerEventData){ | |
dragging = false; | |
} | |
public void OnPointerDown(PointerEventData pointerEventData){ | |
dragging = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment