Gestures

Event and data-type reference for TouchGestureListener's pan and pinch gestures, plus the VelocityTracker that backs their velocity fields.

on this page

TouchGestureListener (MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler) exposes six public UnityEvent<T> fields — not C# events. Wire them in the inspector or call .AddListener(...) in code. See overview for setup.

Pan events

Single-finger drag.

FieldEvent typeData type
OnPanStartGesturePanStartEventGesturePanStartData
OnPanGesturePanEventGesturePanData
OnPanEndGesturePanEndEventGesturePanEndData

GesturePanStartData

MemberTypeDescription
pointerPointerEventDataThe pointer that started the pan.
startPositionVector2Position at the start of the pan.

GesturePanData

MemberTypeDescription
pointerPointerEventDataThe pointer being dragged.
positionVector2Current position.
deltaVector2Movement since the last frame.
velocityVector2Current velocity.
initialPositionVector2Position at the start of the pan.

GesturePanEndData

MemberTypeDescription
pointerPointerEventDataThe pointer that was released.
startPositionVector2Position at the start of the pan.
endPositionVector2Position at release.
totalDeltaVector2Net movement from start to end.
totalDistancefloatPath length traveled.
rollingVelocityVector2Averaged velocity, from VelocityTracker, for inertia.
finalVelocityVector2Instantaneous velocity at release.

Pinch events

Two-finger gesture.

FieldEvent typeData type
OnPinchStartGesturePinchStartEventGesturePinchStartData
OnPinchGesturePinchEventGesturePinchData
OnPinchEndGesturePinchEndEventGesturePinchEndData

PinchValues

struct. Captures the two-finger geometry at a point in time.

MemberTypeDescription
distancefloatDistance between the two pointers.
anglefloatAngle between the two pointers, in radians.
originVector2Midpoint between the two pointers.

GesturePinchStartData

MemberTypeDescription
pointer1PointerEventDataFirst pointer.
pointer2PointerEventDataSecond pointer.
initialPinchValuesGeometry when the pinch started.

GesturePinchData

MemberTypeDescription
pointer1PointerEventDataFirst pointer.
pointer2PointerEventDataSecond pointer.
initialPinchValuesGeometry at pinch start.
currentPinchValuesGeometry this frame.
deltaPinchValuesPer-frame change in distance/angle/origin.
scaleFactorfloatcurrent.distance / initial.distance.

GesturePinchEndData

MemberTypeDescription
pointer1PointerEventDataFirst pointer.
pointer2PointerEventDataSecond pointer.
initialPinchValuesGeometry at pinch start.
finalPinchValuesGeometry at release.
totalScaleFactorfloatfinal.distance / initial.distance.
totalRotationfloatNet rotation, in radians.
rollingOriginVelocityVector2Averaged velocity of the pinch origin, from VelocityTracker, for inertia.
finalOriginVelocityVector2Instantaneous velocity of the pinch origin at release.
HEADS UP

delta means different things on the two gestures. GesturePanData.delta is a Vector2 — a movement vector. GesturePinchData.delta is a PinchValues — the per-frame change in distance, angle, and origin. Reading one as if it were the other is a type error the compiler catches, but the naming collision is easy to trip over when skimming code.

Example

csharp
using UnityEngine;
using Bluecadet.Touchscreen;

public class GestureHandler : MonoBehaviour {
    public TouchGestureListener gestureListener;

    void Start() {
        gestureListener.OnPanStart.AddListener(OnPanStart);
        gestureListener.OnPan.AddListener(OnPan);
        gestureListener.OnPanEnd.AddListener(OnPanEnd);

        gestureListener.OnPinchStart.AddListener(OnPinchStart);
        gestureListener.OnPinch.AddListener(OnPinch);
        gestureListener.OnPinchEnd.AddListener(OnPinchEnd);
    }

    void OnPanStart(GesturePanStartData data) {
        Debug.Log($"Pan started at {data.startPosition}");
    }

    void OnPan(GesturePanData data) {
        transform.position += (Vector3)data.delta;
    }

    void OnPanEnd(GesturePanEndData data) {
        // data.rollingVelocity is a smoothed velocity, suited to feeding a
        // decay/inertia animation on release.
        Debug.Log($"Pan ended, rolling velocity {data.rollingVelocity}");
    }

    void OnPinchStart(GesturePinchStartData data) {
        Debug.Log($"Pinch started, distance {data.initial.distance}");
    }

    void OnPinch(GesturePinchData data) {
        transform.localScale = Vector3.one * data.scaleFactor;
        // data.delta is PinchValues here, not a Vector2.
        float rotationDeltaDegrees = data.delta.angle * Mathf.Rad2Deg;
        transform.Rotate(Vector3.forward, rotationDeltaDegrees);
    }

    void OnPinchEnd(GesturePinchEndData data) {
        Debug.Log($"Pinch ended, total scale {data.totalScaleFactor}");
    }
}

VelocityTracker

VelocityTracker<T> backs the rolling-velocity fields above (GesturePanEndData.rollingVelocity, GesturePinchEndData.rollingOriginVelocity) and is usable directly for other inertia effects.

csharp
public VelocityTracker(int sampleCount, Func<T,T,T> add, Func<T,T,T> subtract, Func<T,float,T> scale, T zero);
MemberDescription
void Track(T position, float time)Record a position sample.
void TrackVelocity(T velocity, float time)Record a velocity sample directly.
T GetLastVelocity(float currentTime = -1f, float maxAge = 0.1f)Most recent velocity, ignoring samples older than maxAge.
T GetAveragedVelocity(float currentTime = -1f, float maxAge = 0.1f)Velocity averaged over samples younger than maxAge.
void Clear()Discard all samples.

Convenience subclasses supply the add/subtract/scale/zero arguments for common types:

TypeConstructor
VelocityTracker2DVelocityTracker2D(int sampleCount = 5)
VelocityTracker3DVelocityTracker3D(int sampleCount = 5)
VelocityTracker1DVelocityTracker1D(int sampleCount = 5)
csharp
var tracker = new VelocityTracker2D();

void Update() {
    tracker.Track(transform.position, Time.time);
}

void OnRelease() {
    Vector2 velocity = tracker.GetAveragedVelocity(Time.time);
    // Feed velocity into a decay/spring for inertia.
}