Hololens开发笔记之Gesture手势识别(基本介绍)

时间:2023-03-09 05:55:07
Hololens开发笔记之Gesture手势识别(基本介绍)

手势识别是HoloLens交互的重要输入方法之一。HoloLens提供了底层API和高层API,可以满足不同的手势定制需求。底层API能够获取手的位置和速度信息,高层API则借助手势识别器来识别预设的手势(包括,单击、双击、长按、平移等等)。

手势输入 Gesture Input


本部分为高级API使用,通过输入源来识别手势。每个手势对应一个SourceKind输入源,大部分手势事件都是系统预设的事件,有些事件会提供额外的上下文信息。

只需要很少的步骤就能使用GestureRecognizer集成手势识别:

  1. 创建GestureRecognizer实例
  2. 注册指定的手势类型
  3. 订阅手势事件
  4. 开始手势识别
private GestureRecognizer recognizer;

    // Use this for initialization
void Start()
{
// 创建GestureRecognizer实例
recognizer = new GestureRecognizer();
// 注册指定的手势类型
recognizer.SetRecognizableGestures(GestureSettings.Tap | GestureSettings.Hold);
// 订阅手势事件
recognizer.HoldStartedEvent += Recognizer_HoldStartedEvent;
recognizer.TappedEvent += Recognizer_TappedEvent;
//开始手势识别
recognizer.StartCapturingGestures();
} private void Recognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay)
{
Debug.Log("Tap");
} private void Recognizer_HoldStartedEvent(InteractionSourceKind source, Ray headRay)
{
Debug.Log("hold");
} // Update is called once per frame
void Update()
{ } void OnDestory()
{
recognizer.TappedEvent -= Recognizer_TappedEvent;
recognizer.HoldStartedEvent -= Recognizer_HoldStartedEvent; }

停止手势识别

recognizer.StopCapturingGestures();

底层API Interaction Input


底层API运行获得输入来源的更多详细信息,例如它在世界中的位置和速度。

如何处理底层交互事件

使用底层交互是很容易的:

  1. 注册InteractionManager事件
  2. 处理事件

停止它也很容易:

  1. 取消注册事件

注册底层交互事件

对于每种你需要的底层事件,你都需要去注册它

InteractionManager.SourcePressed += InteractionManager_SourcePressed;

处理底层交互事件

一旦注册了底层交互事件,在事件发生时你就可以得到回调。你可以使用获取到的时间信息来处理应用行为。

void InteractionManager_SourcePressed(InteractionSourceState state)
{
// state变量里包含以下信息:
// 当前凝视射线信息
// 来源是否被点击
// 位置、速度之类的属性
// 来源id和来源类型 (hand, voice, controller或其他)
}

如何停止交互事件

当你不再想要关注一些事件后,只需要取消时间注册即可。

InteractionManager.SourcePressed -= InteractionManager_SourcePressed;

输入源变化事件

这些事件描述了输入源的当前状态:

  • detected(即将激活)
  • lost(即将取消激活)
  • updates(移动或者一些状态在变化)
  • is pressed(点击、按钮按下或者语音选中)
  • is released(点击结束,按钮松开,语音选中结束)

输入源状态

每个事件都会有一个InteractionSourceState参数,这个参数代表了实时输入源状态:

  • 是否是点击状态
  • InteractionSourceProperties包含了输入源位置信息 InteractionSourceLocation,能够获得当前输入源位置和速度信息
  • 凝视射线信息,用于判断事件发生时用户是否在注视目标
  • 来源类型信息,包括hand、voice、controller或者其他类型

示例代码

using UnityEngine.VR.WSA.Input;

void Start ()
{
InteractionManager.SourceDetected += InteractionManager_SourceDetected;
InteractionManager.SourceUpdated += InteractionManager_SourceUpdated;
InteractionManager.SourceLost += InteractionManager_SourceLost;
InteractionManager.SourcePressed += InteractionManager_SourcePressed;
InteractionManager.SourceReleased += InteractionManager_SourceReleased;
} void OnDestroy()
{
InteractionManager.SourceDetected -= InteractionManager_SourceDetected;
InteractionManager.SourceUpdated -= InteractionManager_SourceUpdated;
InteractionManager.SourceLost -= InteractionManager_SourceLost;
InteractionManager.SourcePressed -= InteractionManager_SourcePressed;
InteractionManager.SourceReleased -= InteractionManager_SourceReleased;
} void InteractionManager_SourceDetected(InteractionSourceState state)
{
// 识别到来源
} void InteractionManager_SourceLost(InteractionSourceState state)
{
//丢失来源
} void InteractionManager_SourceUpdated(InteractionSourceState state)
{
//来源被更新
} void InteractionManager_SourcePressed(InteractionSourceState state)
{
//来源被按下
} void InteractionManager_SourceReleased(InteractionSourceState state)
{
// 来源被松开
}