Kategóriák: Minden - detection - library - functions - scripts

a Mike Ton 12 éve

496

FingerGestures

In the context of gesture detection within software applications, various types of finger gestures are utilized, including pinch, rotation, tap, swipe, and long press. To manage these gestures, a system of functions is implemented to enable and disable event subscriptions.

FingerGestures

FingerGestures

how to detect gesture

library
diagram

toolbox

toolbox scripts include a drag & drop system, third person orbit and pinch-zoom camera script

set of higher-level utility scripts

core system

comp

interpreter

GestureRecognizer

trigger gesture event

getfinger.position

OnFingersUpdated(Listener)

singleton

stream

unsubscribe

When subscribing to events, it's also important to remember to unsubscribe from them when you no longer can or need/want to receive notifications from them. It is particularly important in the case of the FingerGesture singleton events, as it persists between scene transitions. If you do not unsubscribe from the events before your object is destroyed, the FingerGestures singleton will still have that object on its list of event recipients, and a null reference error will occur as a consequence. That is why we unsubscribe from the drag events in the OnDisable() method that is automatically called by the Unity engine when the script is disabled (which also happens before the host game object is destroyed).

function OnDisable(){FingerGestures.OnDragBegin -= FingerGestures_OnDragBegin;}

subscribe

...

Here, we subscribe to the 3 drag events in the OnEnable() method that is automatically called by Unity when our script gets enabled. The ”+=” notation is a syntactic sugar specific to .NET events. It simply means “call this method when that event is fired”. Here for instance, we tell the system to call the FingerGestures_OnDragBegin() method on our TutorialMethod1 instance when the OnDragBegin event located on the FingerGestures singleton is fired. In more technical terms, this FingerGestures_OnDragBegin() method is referred to as a callback method or event handler.

The signature of the callback method you use to subscribe to an event must match the signature of this particular event. For instance, the OnDragEnd event expects a callback method with exactly one argument of type “Vector2” (for the finger position). Trying to subscribe to the OnDragEnd event using a method with a different signature will generate a compilation error.

function OnEnable(){FingerGestures.OnDragBegin += FingerGestures_OnDragBegin;}

DragAndDrop

.OnDragEnd

//dragObject = null; no longer dragging anything

//resume sim

.OnDragMove

//center object on finger

//convert finger position to world position

.OnDragBegin

//disable sim and gravity

//pick dragObject with raycast

Input

Touches

Mouse

=

mousePosition

main chunk of the library and provides input detection through various gesture recognizers

functions
MyFingerGestures_OnTap(fingerPos:Vector2)

Debug.Log( "TAP detected at " + fingerPos );

// The tap event handler we registered in OnEnable() method above. It is called when a tap occurs.

OnDisable()

FingerGestures.OnTap -= MyFingerGestures_OnTap;

//unsubscribe to global default tap event

OnEnable()

FingerGestures.OnTap += MyFingerGestures_OnTap;

//subscribe to global default tap event

types

pinch/rotation
long press
swipe
tap