Swipe Manager - edcasillas/unity-common-utils GitHub Wiki

A singleton game object to detect taps and swipes on mobile devices.

Setup

Add an emtpy game object to the scene where you intend to use the Swipe Manager and tweak the values accordingly.

config

  • MaxFingers: How many fingers should the swipe manager detect.
  • Sensibility: Can be tweaked for both axes.
  • Smooth Rewind Horizontal/Vertical Time: Set this value to anything greater than zero to make swipes act like an analog joystick that smoothly returns to zero, instead of jumping from its current value to zero when the swipe ends.

If you don't add a SwipeManager Component to any game object, a new game object will be created when it's first used. This class guarantees that .Instance will always have a valid value.

API / Usage

The SwipeManager.Instance singleton provides access to the following properties and methods:

  • TouchStarted: Gets a value indicating whether a touch has started with any finger.
  • TouchEnded: Gets a value indicating whether a touch has ended with any finger.
  • Swiping: Gets a value indicating whether the user is swiping with any finger.
  • SwipingHorizontal: Gets a value indicating whether the user is swiping horizontal with any finger.
  • SwipingVertical: Gets a value indicating whether the user is swiping vertical with any finger.
  • SwipingLeft: Gets a value indicating whether the user is swiping left with any finger.
  • SwipingRight: Gets a value indicating whether the user is swiping right with any finger.
  • SwipingUp: Gets a value indicating whether the user is swiping up with any finger.
  • SwipingDown: Gets a value indicating whether the user is swiping down with any finger.
  • Tapped: Gets a value indicating whether the user tapped with any finger.
  • SwipeHorizontal: Indicates the current swiping distance on X axis for each of the supported fingers.
  • SwipeVertical: Indicates the current swiping distance on Y axis for each of the supported fingers.
  • GetSwipeAmountHorizontalForFinger(fingerId, relative): Gets the swipe amount value on the horizontal axis for a specific finger.
  • GetSwipeAmountVerticalForFinger(fingerId, relative): Gets the swipe amount value on the vertical axis for a specific finger.

debug

Example

This code snippet has been taken from YAIR!, and is used in the PlayerController class to make the player jump or switch lanes according to the player's input (swipe up/left/right):

	private void LateUpdate() {
		if(!isJumping && SwipeManager.Instance.SwipingUp) {
			jump();
		}
		if(!isJumping && currentLane == previousLane) {
			if(SwipeManager.Instance.SwipingLeft) {
				if(currentLane > -1) {
					currentLane--;
				}
			} else if(SwipeManager.Instance.SwipingRight) {
				if(currentLane < 1) {
					currentLane++;
				}
			}
		}
	}