Unity3D FingerGesture plug-in

Source: Internet
Author: User
Tags ontap

Directory (?) [-]

  1. Directory
  2. FingerGestures Package Structure
  3. FingerGestures example list
  4. Set scenario
  5. Recognition of a tap gesture
  6. Tutorial gesture Reader
  7. Tutorial: Click Gesture Recognition
  8. Tutorial: drag a gesture Reader
  9. Tutorial slide gesture Reader
  10. Tutorial long-press gesture Reader
  11. Tutorial scaling gesture Reader
  12. Tutorial rotating Gesture Recognition
  13. Tutorial custom Gesture Recognition
  14. How to identify finger events
  15. We recommend that you use the net proxy event.
  16. Directory
  17. FingerGestures Package Structure
  18. FingerGestures example list
  19. Set scenario
  20. Recognition of a tap gesture
  21. Tutorial gesture Reader
  22. Tutorial: Click Gesture Recognition
  23. Tutorial: drag a gesture Reader
  24. Tutorial slide gesture Reader
  25. Tutorial long-press gesture Reader
  26. Tutorial scaling gesture Reader
  27. Tutorial rotating Gesture Recognition
  28. Tutorial custom Gesture Recognition
  29. How to identify finger events
  30. We recommend that you use the net proxy event.
Directory
  • FingerGestures Package Structure
  • FingerGestures example list
  • Set scenario
  • Tutorial: recognize a tap gesture
  • Tutorial: gesture Reader
  • Tutorial: Click Gesture Recognition
  • Tutorial: drag a gesture identification device
  • Tutorial: slide gesture Reader
  • Tutorial: Long-pressed gesture Reader
  • Tutorial: zoom gesture Reader
  • Tutorial: rotating Gesture Recognition
  • Tutorial: Custom Gesture Recognition
  • Tutorial: recognition gesture events
  • Suggestion: Use the. net proxy event
FingerGestures Package Structure
Path, relative to Assets/Plugin /... Description
FingerGestures/ Root directory of the plug-in
FingerGestures/Prefabs You can drag and drop a resource (prefabs) to a scenario)
FingerGestures/Scripts Core scripts and components
FingerGestures/Scripts/Gesture Recognizers Script for each Gesture Recognition
FingerGestures/Scripts/Finger Event Detectors Script for each touch event Detector
FingerGestures/Scripts/Components Additional components to be added for Gesture Recognition and touch events
FingerGestures/Toolbox FingerGestures toolbox script
FingerGestures/Samples. unitypackage Sub-packages of all examples
FingerGestures/PlayMaker Actions. unitypackage FingerGestures plug-in for PlayMaker Extension
Editor/FingerGestures FingerGestures extension to the editor

FingerGestures example list
  • Finger Event (mouse or Finger Event)
    FingerEventsPart1:
    Shows how to use different FingerEventDetectors to detect the down and up of the mouse or finger, stationary and hover events.
    FingerEventsPart2:FingerMotionDetector ).

  • Gestures)
    BasicGestures:
    React to tap, double-click, drag, long-press, and swipe.
    PinchAndTwist:A gesture event is triggered when two or more fingers squeeze or twist on the touch screen at the same time. (PS: usually used for scaling or rotating)
    PointCloudGestures:Demonstrate how to recognize a point cloud gesture. (PS: usually refers to the use of user-drawn patterns for identification)

  • Toolbox)
    Camera (script for storing the Camera ):

    Toolbox-DragView: display and useTBDragViewScript to achieve the drag angle.
    Toolbox-Orbit: demonstrate usageTBOrbitScript to rotate the angle of view around the target.
    Toolbox-Pan: Display usageTBPanScript to rotate the Angle of View on its own axis.
    Toolbox-PinchZoom: demonstrate usageTBPinchZoomScript to achieve zoom.

    Object-Based (scripts for objects in common scenarios ):
    Toolbox-Drag: demonstrate usageTBDragScript to implement simple object dragging
    Toolbox-Hover: demonstrate usageTBHoverChangeMaterialAndTBHoverChangeScaleScript to respond when the mouse or finger is hovering over the object. (PS: similar to putting the mouse on the icon, the icon is shining)
    Toolbox-PinchToScaleTBPinchToScaleScript to scale objects
    Toolbox-TwistToRotate: demonstrate usageTBTwistToRotateScript for rotating objects

Set scenario

You need to instantiate a FingerGesture component in the scenario. The role of FingerGesture in a project is to manage user input and recognition of gesture and mouse or finger events.
There are two ways to add: one is to directly put Plugins \ FingerGestures \ Prefabs underFingerGesturesDrag the prefab file into the scenario. Second, you can create an empty object andFingerGesturesAdd the component.

UseMake PersistentThe flag can make the FingerGestures Singleton valid after the cross-scenario, so it is sufficient to ensure that it is set in the first scenario.

Tutorial: recognize a tap gesture

This section describes how to recognize a simple click action, click action recognition for a special object, and then recognize double-click actions for three fingers.

  • Initialization
    Step 1: set it in the previous chapter;
    Step 2: Create a GameObject namedGestures;
    Step 3:GesturesAddTapRecognizerAnd keep the default settings. You can search for the Component on the project panel or directly open Component> FingerGestures> Gestures> Tap menu item.

    TapRecognizer is one of the gesture identifiers used to monitor user input and work when a valid click action is identified.
    Step 4: Create a new C # script calledTapTutorialAnd add it toGestures.

  • Tap the screen
    Step 1: ClickCopy Event To ClipboardButton to copy the time signal code required by TapGesture to the clipboard.
    Step 2, pasteTapTutorialThe script is as follows:

      public class TapTutorial : MonoBehaviour    {    void OnTap( TapGesture gesture )    {    /* your code here */    }    }    

    OnTapFunction matching defines the information name attribute defined in the TapRecognizer component. When the reader wants to recognize a tap gesture, it uses the unity3dSendMessage APIBroadcast to all scripts in the Gestures object firstOnTapInformation, As long as TapTutorial is bound to this object, itsOnTapThe function will be called.
    For performance consideration, the. net standard event model is usually used to replace the SendMessage API of unity3d.
    Step 3: ModifyOnTopFunction:

      void OnTap( TapGesture gesture )    {    Debug.Log( "Tap gesture detected at " + gesture.Position +    ". It was sent by " + gesture.Recognizer.name );    }    

    gestureParameters contain gesture event data. In the above Code, we mainly output location andTapRecognizerInternal work events. You can alsogestureObtain more attributes in the parameter, for examplegesture.FingersObtains the list of mouse or finger-related gestures. You can also usegesture.SelectionObtain which scenario is being tapped.
    Step 4: perform the test. You can view the debug information output by hitting different positions.

Tutorial: gesture Reader

In FingerGestureGestureRecognizersIt processes user actions that are identified and matched in sequence.

  • Find GestureRecognizers 
    Each gesture has its own script.Plugins\FingerGestures\Scripts\Gesture Recognizers.
    You can alsoComponent > FingerGestures > Gestures menu.

  • Basic usage 
    To recognize a special gesture, you need:
    1. Add the correspondingGestureRecognizerComponents to target objects in the scenario.
    2. configure its properties.
    3. Listen to its gesture events and corresponding responses.

    As part of a gesture event notification,GestureRecognizerTransmits an event parameter that contains the relevant information (location, touch-screen point list of fingers, selected scenario objects, and related GestureRecognizer.

    A gesture reader can listen to events in the following ways:
    1. using the standard. net delegate event model, each gesture reader exposes a. net event interface.
    2. Use unity'sSendMessage()Function:

    Gesture events will be broadcast to all the components in the game object. A gesture event can also point to the current scene object. In this case, you need to configure the gesture identification device to the 'raycaster' component to detect the scene object.

    It depends on your choice .. Net event model is more efficient, unity'sSendMessage()Convenient.

  • Attribute 
    The gesture identifiers derived from the same base class share a common configuration and some functions. For example, we can seeTapRecognizerAndSwipeRecognizerThe component configuration is placed in the same object.
     
    Settings:
    As you can see, the two components share a part of the Configuration:fingers setup,reset mode,event notification settings,references to additional components...
    Similarly, each gesture reader has its own unique configuration. For example, the slide reader must set the distance threshold, speed, and deviation. The maximum duration can be set for multi-touch.

    Event information broadcast:
    Used hereSendMessage()Function to notify other systems. You can useMessage NameAttribute to specify the response function name.
    Generally,Message TargetThe gesture identification component you added will be set. But you can also set other objects.

    Components:
    You can manually add components. For example, addScreenRaycasterThe component allows the gesture reader to know the collision of objects in the scenario. And send the message to the listener. It allows the identifier to forward messages to an associated scenario object.

Tutorial: Click Gesture Recognition

  • Attribute 
    Required Taps: The number of consecutive clicks.
    Max Delay Between Taps: The maximum interval between two clicks. (Seconds)
    Movement Tolerance: The deviation between consecutive tapping and the position of the first tapping.
    Max Duration: Maximum recognizable hand index.

  • Event

    Void OnTap (TapGesture gesture) {// Number of clicks int taps = gesture. Taps ;}
Tutorial: drag a gesture identification device

  • Attribute 
    Movement Tolerance: The identification device is triggered only when the smallest drag distance is reached.
    Apply Same Direction Constraint: It can only be used for Multi-Point drag. After it is opened, if all vertices are not dragged in the same direction, the reader will not recognize it.

  • Event

    Void OnDrag (DragGesture gesture) {// The current Reader stage (Started/Updated/Ended) ContinuousGesturePhase phase = gesture. phase; // drag/move the last frame Vector2 deltaMove = gesture. deltaMove; // complete drag data Vector2 totalMove = gesture. totalMove ;}
Tutorial: slide gesture Reader

  • Attribute 
    Min Distance: The minimum sliding distance.
    Max Distance: Maximum sliding distance.
    Min Velocity: Minimum sliding speed.
    Max Deviation: Maximum allowable angle deviation. (Degree)

  • Event

    Void OnSwipe (SwipeGesture gesture) {// complete slide data Vector2 move = gesture. move; // float velocity = gesture. velocity; // The approximate moving direction FingerGestures. swipeDirection direction = gesture. direction ;}
Tutorial: Long-pressed gesture Reader

  • Attribute 
    Press Duration: The minimum duration.
    Move Tolerance: Maximum movement deviation allowed during long-pressing.

  • Event

    Void OnLongPress (LongPressGesture gesture) {// float elapsed = gesture. ElapsedTime ;}
Tutorial: zoom gesture Reader

  • Attribute 
    Minimum DOT: Allowed dot product of a small vector.
    Minimum Distance: The shortest path allowed by the two fingers during the first touch screen.

  • Event

    Void OnPinch (PinchGesture gesture) {// the current status of the reader (Started/Updated/Ended) ContinuousGesturePhase phase = gesture. phase; // float gap = gesture between the current two fingers. gap; // the current and previous change value float delta = gesture. delta ;}
Tutorial: rotating Gesture Recognition

  • Attribute 
    Minimum DOT: Allowed dot product of a small vector.
    Minimum Rotation: Required minimum spin angle.

  • Event

    Void OnTwist (TwistGesture gesture) {// the current status of the reader (Started/Updated/Ended) ContinuousGesturePhase phase = gesture. phase; // The Last angle change (degree) float delta = gesture. deltaRotation; // overall angle variation (degree) float total = gesture. totalRotation ;}
  • Desktop Simulation 
    In the desktop environment, you canleft-CTRLPress the mouse wheel to adjust the angle. You can alsoMouse Input ProviderConfigure other buttons.

Tutorial: Custom Gesture Recognition

Since FingerGestures 3.0PointCloudRecognizerRecognize custom gestures. Implemented using a gesture matching algorithm based on $ P recognizer. Currently, only single-Finger Recognition is supported. In the future, multi-point custom gestures will be supported.
 
The point cloud identification tool compares the user input with the configured gesture template. Then, it returns the closest matching result and the matching score and gap value.
Point Cloud identifiers are of a fixed scale and direction, which means that they can be identified in a relatively large, small, or backward direction (Li RuO: from left to right ).

  • Point Cloud reader Template 
    A template contains the data of the gesture to be recognized. Is edited in an editor.
     
    To create a template, follow these steps:
    1: Right-click your project code and choose create> PonitCloud Gesture Template.
     
    You can add a new template to the project and rename it by yourself.
    2: select a template and click Edit in the Inspecrot panel.
     
    3: start to draw a pattern.

  • Use the point cloud Identification Tool 
    Step 1:
    1: Make sure that the finger gesture attribute has been set for the scenario object.
    2: Create a NewGesturesObject.
    3: AddPointCloudRecognizerComponent.
     
    Pay special attention to the following attributes.
    Max Match Distance: Control the accuracy of recognition. The lower the value, the more accurate it is.
    Sampling Distance: The minimum distance between two points during coherent motion sampling. The smaller the value, the more precise the sampling.
    Gesture Templates List: List of specified templates.

    Step 2:
    Add the template you just created and drag it to the gesture template list.

    Step 3:
    1. Create a c # file namedPointCloudTutorial.cs.
    2.PointCloudRecognizerCreate a gesture object.
    3. Edit the c # file:

      public class PointCloudTutorial : MonoBehaviour    {    void OnCustomGesture( PointCloudGesture gesture )    {    Debug.Log( "Recognized custom gesture: " + gesture.RecognizedTemplate.name +    ", match score: " + gesture.MatchScore +    ", match distance: " + gesture.MatchDistance );    }    }    

    Gesture events protect the following important attributes:
    gesture.RecognizedTemplate: It is considered as the best matching gesture template.
    gesture.MatchScore: The value of a percentage, indicating the matching degree.
    gesture.MatchDistance: An absolute value of measurement, indicating the matching degree.
    You can also use other gesture attributes. For example, location and select object attributes.

  • Use code to create a template 
    You can use the api word editor extension to create a gesture template at runtime.

      void Awake()    {    PointCloudGestureTemplate triangle = ScriptableObject.CreateInstance<PointCloudGestureTemplate>();    triangle.name = "Triangle Gesture Template";    triangle.BeginPoints();    triangle.AddPoint( 0, 1, 1 );    triangle.AddPoint( 0, 2, 2 );    triangle.AddPoint( 0, 3, 1 );    triangle.AddPoint( 0, 1, 1 );    triangle.EndPoints();    PointCloudGestureTemplate square = ScriptableObject.CreateInstance<PointCloudGestureTemplate>();    square.name = "Square Gesture Template";    square.BeginPoints();    square.AddPoint( 0, 2, 1 );    square.AddPoint( 0, 2, 3 );    square.AddPoint( 0, 4, 3 );    square.AddPoint( 0, 4, 1 );    square.AddPoint( 0, 2, 1 );    square.EndPoints();    PointCloudRegognizer recognizer = gameObject.AddComponent<PointCloudRegognizer>();    recognizer.AddTemplate( triangle );    recognizer.AddTemplate( square );    }    

    First ParameterAddPointIs the order of strokes. This api currently only supports single-line strokes.
    WhenEndPoints()During the call, the gesture template will be formatted and all vertices will be re-drawn into a number ranging from 0 to 1.

Tutorial: Identifying finger events

FingerGestures can recognize single-point input gestures, such as Up, down, hovering, moving, and long-pressed. VariousFingerEventDetectorThe component is used to identify the corresponding finger event, andGestureRecognizersSimilarly, it is triggered through broadcast information.

  • FingerEventDetector 
    All finger event identifiers are derived from a basic abstract class. GenerallyFingerEventDetectorThe instance monitors all finger event signals. You can also configureFinger Index FilterProperty to track only specific finger events.
    Similar to the gesture reader, the finger event reader transmits an event data object, which is derived fromFingerEventClass, including the following attributes:
Attribute Type Description
Name String Message name
Detector FingerEventDetector Finger event identifier in this event
Finger FingerGestures. Finger Finger class in this event
Position Vector2 Event Location
Selection GameObject Selected game objects (dependent on the 'screenraycaster' component)
Hit/td> RaycastHit Ray projection collision, provided by 'screenraycaster', is useful for normal display.

  • FingerUpDetector

    Void OnFingerUp (FingerUpEvent e) {// float elapsedTime = e. TimeHeldDown ;}
  • FingerHoverDetector

    Void OnFingerHover (FingerHoverEvent e) {// check the status, whether to enter or exit. 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 
    The identifier can recognize two types of events.
    1. OnFingerMove: When the finger position changes from the previous frame.
    2. OnFingerStationary: When the finger is at the same position as the previous 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 );    }    
Suggestion: Use the. net proxy event

When unitySendMessage()Function broadcast event messages are very convenient, but the efficiency is low and the. NET proxy event is not flexible enough.
*Gesture Events 
Each gesture reader exposes a publicOnGesture. NET events that match gesture events and finger events. Usage and usageSendMessage()Same.

[RequireComponent (typeof (TapGesture)] public class TapTutorial: MonoBehaviour {void Start () {// search for the lightweight Event Identification Tool TapRecognizer tap = GetComponent <TapRecognizer> (); // subscribe to its. NET event tap. onGesture + = MyTapEventHandler;} void MyTapEventHandler (TapGesture gesture) {Debug. log ("Tap detected at" + gesture. position );}}

Sometimes you need to stop listening events. You can use the following methods:

    tap.OnGesture -= MyTapEventHandler;

Note that the lifecycle of related objects when listening events are stopped may cause memory leakage, which is a trap of. NET proxy events.

Another method is,FingerGesturesThe Singleton exposes a globalOnGestureEventHook to listen to any gesture events.

        void Start(){FingerGestures.OnGestureEvent += FingerGestures_OnGestureEvent;}void FingerGestures_OnGestureEvent( Gesture gesture ){Debug.Log( gesture.Recognizer.name + " fired its gesture event" );if( gesture is TapGesture )Debug.Log( "Tapped: " + ((TapGesture)gesture).Taps );}
  • Finger Event 
    Similar to the above.

        FingerUpDetector.OnFingerUp( FingerUpEvent e )    FingerDownDetector.OnFingerDown( FingerDownEvent e )    FingerHoverDetector.OnFingerHover( FingerHoverEvent e )    FingerMotionDetector.OnFingerMove( FingerMotionEvent e )    FingerMotionDetector.OnFingerStationary( FingerMotionEvent e )    FingerGestures.OnFingerEvent( FingerEvent e )    

-- EOF

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.