forked from MarcoMig/Unity-Swipe-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnity Swipe Detection.cs
53 lines (47 loc) · 1.6 KB
/
Unity Swipe Detection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.EventSystems;
// This class is used to manage the grid UI meaning position the tails, resize them, and create the grid seen by the user
public class GridViewController : MonoBehaviour, IDragHandler, IEndDragHandler
{
#region FIELDS
private Grid grid;
private enum DraggedDirection
{
Up,
Down,
Right,
Left
}
#endregion
#region IDragHandler - IEndDragHandler
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("Press position + " + eventData.pressPosition);
Debug.Log("End position + " + eventData.position);
Vector3 dragVectorDirection = (eventData.position - eventData.pressPosition).normalized;
Debug.Log("norm + " + dragVectorDirection);
GetDragDirection(dragVectorDirection);
}
//It must be implemented otherwise IEndDragHandler won't work
public void OnDrag(PointerEventData eventData)
{
}
private DraggedDirection GetDragDirection(Vector3 dragVector)
{
float positiveX = Mathf.Abs(dragVector.x);
float positiveY = Mathf.Abs(dragVector.y);
DraggedDirection draggedDir;
if (positiveX > positiveY)
{
draggedDir = (dragVector.x > 0) ? DraggedDirection.Right : DraggedDirection.Left;
}
else
{
draggedDir = (dragVector.y > 0) ? DraggedDirection.Up : DraggedDirection.Down;
}
Debug.Log(draggedDir);
return draggedDir;
}
#endregion
}