**學習源於官方文檔 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 手勢輸入員的類別 以及 在 頭部感應器觸發事件的事件~ 而且一些事件來提供了額外的一些信息~
高層的封裝代表着簡潔,方便,快捷~所以我們只要通過下面幾個步驟我們就能實現手勢識別了~
-
創建一個手勢識別對象 Gesture Recognizer
-
設置手勢監聽的類型
-
注冊事件
-
開啟手勢識別的功能
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調用分廠簡單,三步搞定~
-
注冊手勢交互的事件
-
接收事件回調
-
移除手勢交互的事件
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 枚舉類型,可以是不同的輸入類型,包括手勢 聲音 控制器 以及 其他等等~