User Tools

Site Tools


manual:fingers:start

Detecting Finger Events

FingerGestures allows you to detect basic single-finger input events such as:

  • Finger Down
  • Finger Up
  • Finger Hover (enter/exit a collider)
  • Finger Move
  • Finger Stationary (finger is down but motionless)

This is accomplished by using the various FingerEventDetector components, which share some similarities with the GestureRecognizers such as event message broadcasting and the use of an optional ScreenRaycaster component to interact with scene objects.

FingerEventDetector

This is the abstract base class from which all the concrete finger event detectors derive.

By default, each FingerEventDetector instance monitors every single finger and signals when the correct input event is detected. It's also possible to make a FingerEventDetector track a specific finger only by providing that finger's index in the Finger Index Filter property.

Similarly to the GestureRecognizers, the FingerEventDetectors pass a single event data object as argument to the event handler method. This event data object derives from the FingerEvent base class, which exposes the following properties:

Property Type Description
Name string Name of the event as specified in the “Message Name” property (e.g. “OnFingerDown”)
Detector FingerEventDetector A reference to the FingerEventDetector that fired this event
Finger FingerGestures.Finger A reference to the finger involved in this event
Position Vector2 Screen-space position of the event
Selection GameObject GameObject located at the event position (requires ScreenRaycaster component)
Hit RaycastHit Raycast hit result of the last raycast performed by the ScreenRaycaster. Useful to access additional information such as surface normal

FingerDownDetector

Detects when a finger starts touching the screen.

void OnFingerDown( FingerDownEvent e ) 
{
    Debug.Log( e.Finger + " Down at " + e.Position + " on object:" + e.Selection );
}

FingerUpDetector

Detects when a finger has been lifted from the screen.

void OnFingerUp( FingerUpEvent e ) 
{
    // time the finger has been held down before being released
    float elapsedTime = e.TimeHeldDown;
}

FingerHoverDetector

Detects when a finger enters or exits an object in the scene. In order to function properly, the FingerHoverDetector requires a ScreenRaycaster component.

void OnFingerHover( FingerHoverEvent e ) 
{
    // check the hover event phase to check if we're entering or exiting the object
    if( e.Phase == FingerHoverPhase.Enter )
    {
        Debug.Log( e.Finger + " entered object: " + e.Selection );
    }
    else if( e.Phase == FingerHoverPhase.Exit )
    {
        Debug.Log( e.Finger + " exited object: " + e.Selection );
    }
}

FingerMotionDetector

Detects finger motion or lack of motion (stationary). The detector can fire two different events:

  • OnFingerMove when a finger position has changed since last frame
  • OnFingerStationary when a finger has remained at the same position since last frame
void OnFingerMove( FingerMotionEvent e ) 
{
    float elapsed = e.ElapsedTime;
 
    if( e.Phase == FingerMotionPhase.Started )
        Debug.Log( e.Finger + " started moving at " + e.Position);
    else if( e.Phase == FingerMotionPhase.Updated )
        Debug.Log( e.Finger + " moving at " + e.Position );
    else if( e.Phase == FingerMotionPhase.Ended )
        Debug.Log( e.Finger + " stopped moving at " + e.Position );
}
 
void OnFingerStationary( FingerMotionEvent e ) 
{
    float elapsed = e.ElapsedTime;
 
    if( e.Phase == FingerMotionPhase.Started )
        Debug.Log( e.Finger + " started stationary state at " + e.Position );
    else if( e.Phase == FingerMotionPhase.Updated )
        Debug.Log( e.Finger + " is still stationary at " + e.Position );
    else if( e.Phase == FingerMotionPhase.Ended )
        Debug.Log( e.Finger + " stopped being stationary at " + e.Position );
}
manual/fingers/start.txt · Last modified: 2013/03/17 18:31 by wravaine