Gesture recognition is one of the important input methods of HoloLens interaction. HoloLens provides the underlying API and high-level API to meet different gesture customization needs. The underlying API gets the position and speed information of the hand, and the high-level API uses the gesture recognizer to identify preset gestures (including, click, double-tap, long-press, Pan, and so on).
Gesture Input Gesture input
This section is used by the Advanced API to identify gestures through the input source. Each gesture corresponds to a sourcekind input source, and most gesture events are system-preset events, and some events provide additional contextual information.
You can use Gesturerecognizer integrated gesture recognition in a few steps:
- Creating an Gesturerecognizer instance
- Registering the specified gesture type
- Subscribe to gesture Events
- Start gesture recognition
PrivateGesturerecognizer recognizer; //Use this for initialization voidStart () {//Creating an Gesturerecognizer instanceRecognizer =NewGesturerecognizer (); //registering the specified gesture typeRecognizer. Setrecognizablegestures (Gesturesettings.tap |gesturesettings.hold); //Subscribe to gesture EventsRecognizer. Holdstartedevent + =recognizer_holdstartedevent; Recognizer. Tappedevent+=recognizer_tappedevent; //Start gesture Recognitionrecognizer. Startcapturinggestures (); } Private voidRecognizer_tappedevent (Interactionsourcekind source,intTapcount, Ray Headray) {Debug.Log ("Tap"); } Private voidrecognizer_holdstartedevent (interactionsourcekind source, Ray Headray) {Debug.Log (" hold"); } //Update is called once per frame voidUpdate () {}voidondestory () {recognizer. Tappedevent-=recognizer_tappedevent; Recognizer. Holdstartedevent-=recognizer_holdstartedevent; }
Stop gesture recognition
Recognizer. Stopcapturinggestures ();
Underlying API Interaction Input
The underlying API runs to get more detailed information about the input source, such as its location and speed in the world.
How to handle underlying interaction events
It is easy to use the underlying interaction:
- Registering Interactionmanager Events
- Handling Events
It's also easy to stop it:
- Unregister event
Registering the underlying interaction event
For each of the underlying events you need, you need to register it
interactionmanager.sourcepressed + = interactionmanager_sourcepressed;
Handling underlying interaction events
Once the underlying interaction event is registered, you can get a callback when the event occurs. You can use the time information you get to handle app behavior.
void interactionmanager_sourcepressed (interactionsourcestate state) { // The state variable contains the following information: // Current gaze ray information // whether the source was clicked // properties such as position, speed // Source ID and Source type (hand, voice, controller, or other)}
How to stop an interaction event
When you no longer want to focus on some events, just cancel the time register.
interactionmanager.sourcepressed-= interactionmanager_sourcepressed;
Input source Change events
These events describe the current state of the input source:
- Detected (will be activated)
- Lost (deactivation is about to be deactivated)
- Updates (moving or some states are changing)
- is pressed (click, button pressed or voice selected)
- is released (click End, button to release, end of voice check)
Input source Status
Each event will have a interactionsourcestate parameter, which represents the real-time input source state:
- Whether it is a click state
- Interactionsourceproperties contains the input source location information Interactionsourcelocation to obtain the current input source location and speed information
- Gaze ray information to determine whether the user is looking at the target when the event occurs
- Source type information, including hand, voice, controller, or other types
Sample code
usingUnityEngine.VR.WSA.Input;voidStart () {interactionmanager.sourcedetected+=interactionmanager_sourcedetected; Interactionmanager.sourceupdated+=interactionmanager_sourceupdated; Interactionmanager.sourcelost+=Interactionmanager_sourcelost; Interactionmanager.sourcepressed+=interactionmanager_sourcepressed; Interactionmanager.sourcereleased+=interactionmanager_sourcereleased;}voidOnDestroy () {interactionmanager.sourcedetected-=interactionmanager_sourcedetected; Interactionmanager.sourceupdated-=interactionmanager_sourceupdated; Interactionmanager.sourcelost-=Interactionmanager_sourcelost; Interactionmanager.sourcepressed-=interactionmanager_sourcepressed; Interactionmanager.sourcereleased-=interactionmanager_sourcereleased;}voidinteractionmanager_sourcedetected (interactionsourcestate state) {//Identify the source}voidInteractionmanager_sourcelost (interactionsourcestate state) {//lost Source}voidinteractionmanager_sourceupdated (interactionsourcestate state) {//Source is updated}voidinteractionmanager_sourcepressed (interactionsourcestate state) {//the source was pressed}voidinteractionmanager_sourcereleased (interactionsourcestate state) {//The source is loosened .}
HoloLens development Note gesture gesture recognition (basic introduction)