Why is it so hard to find a great swipe manager for Unity ?
A simple swipe event triggers that also works with mouse movement.
Attach the SwipeEventTrigger component to a gameobject, link its OnSwipe event to your scripts function.
Function prototype should take a SwipeData argument, defined in UnityUtility.Events, it contains:
public struct SwipeData
{
public float distance; //The distance of the swipe
public float angle; //The angle of the swipe, 0 is right
public Vector2 direction; //The directional vector of the swipe
public Vector2 start; //The start screen position of the swipe
public Vector2 end; //The end screen position of the swipe
}
Method | Description |
---|---|
SwipeEventTrigger.GetMoveDirection | Returns the up/down/left/right direction |
If you want to use different event behaviour, like calling a separate event for each direction, you can override OnSwipeDetected in inherited class :
protected override void OnSwipeDetected(SwipeData swipe)
{
base.OnSwipeDetected(swipe);
switch (GetMoveDirection(swipe))
{
case MoveDirection.Up:
OnUpSwipe?.Invoke();
break;
case MoveDirection.Down:
OnDownSwipe?.Invoke();
break;
case MoveDirection.Left:
OnLeftSwipe?.Invoke();
break;
case MoveDirection.Right:
OnRightSwipe?.Invoke();
break;
default:
break;
}
}
Example class SwipeDirectionEventTrigger is provided.
Hope you found what you needed, use it as you'd like !