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

学习源于官方文档 Gestures in Unity windows

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


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

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

1、概述

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).  ide

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

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

  • Gesture Input
  • Interaction Input

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

Namespace: UnityEngine.VR.WSA.Inputthis

Types: GestureRecognizer, GestureSettings, InteractionSourceKindspa

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 手势输入员的类别 以及 在 头部感应器触发事件的事件~ 并且一些事件来提供了额外的一些信息~code

高层的封装表明着简洁,方便,快捷~因此咱们只要经过下面几个步骤咱们就能实现手势识别了~

  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;
     }

    }

一切尽在代码中~


3、Interaction Input(核心API)

Namespace: UnityEngine.VR.WSA.Input

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

一、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;
     }

    }

二、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 枚举类型,能够是不一样的输入类型,包括手势 声音 控制器 以及 其余等等~
相关文章
相关标签/搜索