學習源於官方文檔 Gestures in Unity windows
筆記一部分是直接翻譯官方文檔,部分各人理解不一致的和一些比較淺顯的保留英文原文api
HoloLens 有三大輸入系統,凝視點、手勢和聲音 ~ 本文主要記錄手勢識別的學習筆記 ~app
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
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
高層的封裝表明着簡潔,方便,快捷~因此咱們只要經過下面幾個步驟咱們就能實現手勢識別了~
開啓手勢識別的功能
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; }
}
一切盡在代碼中~
Namespace: UnityEngine.VR.WSA.Input
Types: InteractionManager, InteractionSourceState, InteractionSource, InteractionSourceProperties, InteractionSourceKind, InteractionSourceLocation
手勢交互核心層的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; }
}
源碼~
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; } } }