Skip to content

Instantly share code, notes, and snippets.

using UnityEngine;
using UnityEngine.EventSystems;
public class InputManager : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
}
public void OnBeginDrag(PointerEventData eventData)
{
throw new System.NotImplementedException();
}
public void OnDrag(PointerEventData eventData)
{
throw new System.NotImplementedException();
}
public enum Direction { Left, Up, Right, Down, None }
Direction direction;
Vector2 startPos, endPos;
public float swipeThreshold = 100f;
bool draggingStarted;
private void Awake()
{
draggingStarted = false;
direction = Direction.None;
}
public void OnBeginDrag(PointerEventData eventData)
{
draggingStarted = true;
startPos = eventData.pressPosition;
}
public void OnDrag(PointerEventData eventData)
{
if (draggingStarted)
{
endPos = eventData.position;
Vector2 difference = endPos - startPos; // difference vector between start and end positions.
if (difference.magnitude > swipeThreshold)
{
public void OnEndDrag(PointerEventData eventData)
{
if (draggingStarted && direction != Direction.None)
{
//Do something with this direction data.
Debug.Log("Swipe direction: " + direction);
}
//reset the variables
startPos = Vector2.zero;
using UnityEngine;
using UnityEngine.EventSystems;
public class InputManager : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
public enum Direction { Left, Up, Right, Down, None }
Direction direction;
Vector2 startPos, endPos;
using System;
public class InputManager : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
public Action<Direction> onSwipeDetected;
}