(五)Hololens Unity 开发之 手势识别

学习源于官方文档 Gestures in Unity

笔记一部分是直接翻译官方文档,部分各人理解不一致的和一些比较浅显的保留英文原文


(五)Hololens Unity 开发之 手势识别

HoloLens 有三大输入系统,凝视点、手势和声音 ~ 本文主要记录手势识别的学习笔记 ~

一、概述

Gestures are the second "G" in Gaze, Gesture, and Voice. Unity provides both low level access (raw position and velocity information) and a high level gesture recognizer that exposes more complex gesture events (for example: tap, double tap, hold, manipulation and navigation).  

手势识别是Hololens 三大输入系统 Gaze焦点、Gesture手势 和 Voice声音 中的第二个G~(这句翻译的好像有点儿生硬了~)同时在Unity层面也提供了底层的API,例如原始位置速度位移信息等~ 当然,也提供了高维度封装好的供开发者调用的高层API~例如:手势单击,双击,长按,多点点击以及导航栏...(完全按照开发者的思维翻译的~)

官文可以得知~ HoloLens提供了高层次的和核心的API供开发者调用~
2

  • Gesture Input
  • Interaction Input

二、Gesture Input(高层次分装的API)

Namespace: UnityEngine.VR.WSA.Input

Types: GestureRecognizer, GestureSettings, InteractionSourceKind

These high level gestures are generated by input sources. Each Gesture event provides the SourceKind for the input as well as the targeting head ray at the time of the event. Some events provide additional context specific information.

这是一些高层次的手势源,每个手势输入都对应了一个事件event~ 而每个事件都提供了SourceKind 手势输入员的类别 以及 在 头部感应器触发事件的事件~ 而且一些事件来提供了额外的一些信息~

高层的封装代表着简洁,方便,快捷~所以我们只要通过下面几个步骤我们就能实现手势识别了~

  1. 创建一个手势识别对象 Gesture Recognizer
  2. 设置手势监听的类型
  3. 注册事件
  4. 开启手势识别的功能

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.VR.WSA.Input;

    public class GestureDemo : MonoBehaviour {

     private GestureRecognizer recognizer;
    
     // Use this for initialization
     void Start () {
         Debug.Log("初始化开始~");
         // 创建手势识别对象
         recognizer = new GestureRecognizer();
         // 设置手势识别的类型
         recognizer.SetRecognizableGestures(GestureSettings.Tap | GestureSettings.Hold | GestureSettings.DoubleTap);
         // 添加手势识别的事件
         recognizer.TappedEvent += Recognizer_TappedEvent;
         recognizer.HoldStartedEvent += Recognizer_HoldStartedEvent;
         recognizer.HoldCompletedEvent += Recognizer_HoldCompletedEvent;
         recognizer.HoldCanceledEvent += Recognizer_HoldCanceledEvent;
         // 开启手势识别
         recognizer.StartCapturingGestures();
         Debug.Log("初始化完成~");
     }
    
     private void Recognizer_HoldCanceledEvent(InteractionSourceKind source, Ray headRay)
     {
         Debug.Log("Recognizer_HoldCanceledEvent 枚举类型应该为1 ~ " + source);
     }
    
     private void Recognizer_HoldCompletedEvent(InteractionSourceKind source, Ray headRay)
     {
         Debug.Log("Recognizer_HoldCompletedEvent 枚举类型应该为1 ~ " + source);
     }
    
     private void Recognizer_HoldStartedEvent(InteractionSourceKind source, Ray headRay)
     {
         Debug.Log("Recognizer_HoldStartedEvent 枚举类型应该为1 ~ " + source);
     }
    
     private void Recognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay)
     {
         Debug.Log("Recognizer_TappedEvent 枚举类型应该为1 ~ " + source + " 点击的次数 " + tapCount);
     }
    
     void OnDestroy()
     {
         // 销毁注册的事件
         recognizer.TappedEvent -= Recognizer_TappedEvent;
         recognizer.HoldStartedEvent -= Recognizer_HoldStartedEvent;
         recognizer.HoldCompletedEvent -= Recognizer_HoldCompletedEvent;
         recognizer.HoldCanceledEvent -= Recognizer_HoldCanceledEvent;
     }

    }

一切尽在代码中~


三、Interaction Input(核心API)

Namespace: UnityEngine.VR.WSA.Input

Types: InteractionManager, InteractionSourceState, InteractionSource, InteractionSourceProperties, InteractionSourceKind, InteractionSourceLocation

1、API介绍

手势交互核心层的API调用分厂简单,三步搞定~

  1. 注册手势交互的事件
  2. 接收事件回调
  3. 移除手势交互的事件

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.VR.WSA.Input;

    public class InteractionDemo : MonoBehaviour {

     // Use this for initialization
     void Start () {
         // 点击某个控件的事件
         InteractionManager.SourcePressed += InteractionManager_SourcePressed;
         // 点击某个控件放开的事件
         InteractionManager.SourceReleased += InteractionManager_SourceReleased;
         // 选中某个控件的事件
         InteractionManager.SourceDetected += InteractionManager_SourceDetected;
         // 选中后再不选中的事件
         InteractionManager.SourceLost += InteractionManager_SourceLost;
         // 更新某个控件的事件
         InteractionManager.SourceUpdated += InteractionManager_SourceUpdated;
     }
    
     private void InteractionManager_SourceUpdated(InteractionSourceState state)
     {
         throw new System.NotImplementedException();
     }
    
     private void InteractionManager_SourceReleased(InteractionSourceState state)
     {
         throw new System.NotImplementedException();
     }
    
     private void InteractionManager_SourceLost(InteractionSourceState state)
     {
         throw new System.NotImplementedException();
     }
    
     private void InteractionManager_SourceDetected(InteractionSourceState state)
     {
         throw new System.NotImplementedException();
     }
    
     private void InteractionManager_SourcePressed(InteractionSourceState state)
     {
         throw new System.NotImplementedException();
     }
    
     void OnDestroy()
     {
         // 移除各种手势的事件
         InteractionManager.SourceDetected -= InteractionManager_SourceDetected;
         InteractionManager.SourceUpdated -= InteractionManager_SourceUpdated;
         InteractionManager.SourceLost -= InteractionManager_SourceLost;
         InteractionManager.SourcePressed -= InteractionManager_SourcePressed;
         InteractionManager.SourceReleased -= InteractionManager_SourceReleased;
     }

    }

2、InteractionSourceState 结构体

源码~

namespace UnityEngine.VR.WSA.Input
{
    [RequiredByNativeCodeAttribute]
    public struct InteractionSourceState
    {
        public Ray headRay { get; }
        public bool pressed { get; }
        public InteractionSourceProperties properties { get; }
        public InteractionSource source { get; }
    }
}
  • headRay 这个变量可以确定该事件发生时用户头部所处的位置信息
  • pressed 是否处于点击状态
  • InteractionSourceProperties 可以通过InteractionSourceProperties获得输入源的位置、速度以及输入源的时间点
  • InteractionSource 枚举类型,可以是不同的输入类型,包括手势 声音 控制器 以及 其他等等~

转载于:https://www.cnblogs.com/Erma-king/p/6367290.html

你可能感兴趣的:((五)Hololens Unity 开发之 手势识别)