Hololens開發筆記之Gesture手勢識別(單擊,雙擊)


本文使用手勢識別實現識別單擊及雙擊手勢的功能,當單擊Cube時改變顏色為藍色,當雙擊Cube時改變顏色為綠色。

手勢識別是HoloLens交互的重要輸入方法之一。HoloLens提供了底層API和高層API,可以滿足不同的手勢定制需求。底層API能夠獲取手的位置和速度信息,高層API則借助手勢識別器來識別預設的手勢( 包括,單擊、雙擊、長按、平移等等) 。

本部分為高級API使用,通過輸入源來識別手勢。每個手勢對應一個SourceKind輸入源,大部分手勢事件都是系統預設的事件,有些事件會提供額外的上下文信息。只需要很少的步驟就能使用GestureRecognizer集成手勢識別:
1. 創建GestureRecognizer實例
2. 注冊指定的手勢類型
3. 訂閱手勢事件
4. 開始手勢識別

1、添加手勢管理腳本,在Manager上添加腳本GestureManager.cs

GestureManager腳本內容如下,其中注冊了Tapped事件,當發生tap事件時,判斷是單擊還是雙擊事件

// Copyright (c) Microsoft Corporation. All rights reserved.  
// Licensed under the MIT License. See LICENSE in the project root for license information.  
  
using System;  
using UnityEngine;  
using UnityEngine.VR.WSA.Input;  
  
namespace HoloToolkit.Unity  
{  
    /// <summary>  
    /// GestureManager creates a gesture recognizer and signs up for a tap gesture.  
    /// When a tap gesture is detected, GestureManager uses GazeManager to find the game object.  
    /// GestureManager then sends a message to that game object.  
    /// </summary>  
    [RequireComponent(typeof(GazeManager))]  
    public partial class GestureManager : Singleton<GestureManager>  
    {  
        /// <summary>  
        /// Key to press in the editor to select the currently gazed hologram  
        /// </summary>  
        public KeyCode EditorSelectKey = KeyCode.Space;  
  
        /// <summary>  
        /// To select even when a hologram is not being gazed at,  
        /// set the override focused object.  
        /// If its null, then the gazed at object will be selected.  
        /// </summary>  
        public GameObject OverrideFocusedObject  
        {  
            get; set;  
        }  
  
        /// <summary>  
        /// Gets the currently focused object, or null if none.  
        /// </summary>  
        public GameObject FocusedObject  
        {  
            get { return focusedObject; }  
        }  
  
        private GestureRecognizer gestureRecognizer;  
        private GameObject focusedObject;  
  
        public bool IsNavigating { get; private set; }  
        public Vector3 NavigationPosition { get; private set; }  
  
        void Start()  
        {  
            //  創建GestureRecognizer實例  
            gestureRecognizer = new GestureRecognizer();  
            //  注冊指定的手勢類型,本例指定單擊及雙擊手勢類型  
            gestureRecognizer.SetRecognizableGestures(GestureSettings.Tap  
                | GestureSettings.DoubleTap);  
            //  訂閱手勢事件  
            gestureRecognizer.TappedEvent += GestureRecognizer_TappedEvent;  
  
            //  開始手勢識別  
            gestureRecognizer.StartCapturingGestures();  
        }  
  
        private void OnTap()  
        {  
            if (focusedObject != null)  
            {  
                focusedObject.SendMessage("OnTap");  
            }  
        }  
  
        private void OnDoubleTap()  
        {  
            if (focusedObject != null)  
            {  
                focusedObject.SendMessage("OnDoubleTap");  
            }  
        }  
  
        private void GestureRecognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay)  
        {  
            if (tapCount == 1)  
            {  
                OnTap();  
            }  
            else  
            {  
                OnDoubleTap();  
            }  
        }  
  
        void LateUpdate()  
        {  
            GameObject oldFocusedObject = focusedObject;  
  
            if (GazeManager.Instance.Hit &&  
                OverrideFocusedObject == null &&  
                GazeManager.Instance.HitInfo.collider != null)  
            {  
                // If gaze hits a hologram, set the focused object to that game object.  
                // Also if the caller has not decided to override the focused object.  
                focusedObject = GazeManager.Instance.HitInfo.collider.gameObject;  
            }  
            else  
            {  
                // If our gaze doesn't hit a hologram, set the focused object to null or override focused object.  
                focusedObject = OverrideFocusedObject;  
            }  
  
            if (focusedObject != oldFocusedObject)  
            {  
                // If the currently focused object doesn't match the old focused object, cancel the current gesture.  
                // Start looking for new gestures.  This is to prevent applying gestures from one hologram to another.  
                gestureRecognizer.CancelGestures();  
                gestureRecognizer.StartCapturingGestures();  
            }  
 
#if UNITY_EDITOR  
            if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(EditorSelectKey))  
            {  
                OnTap();  
            }  
#endif  
        }  
  
        void OnDestroy()  
        {  
            gestureRecognizer.StopCapturingGestures();  
            gestureRecognizer.TappedEvent -= GestureRecognizer_TappedEvent;  
        }  
    }  
}  

2、在Cube上添加處理腳本CubeScript.cs

CubeScript腳本如下,定義兩個方法,OnTap將Cube的顏色設置為藍色, OnDoubleTap將Cube的顏色設置為綠色

using UnityEngine;  
using System.Collections;  
  
public class CubeScript : MonoBehaviour {  
  
    // Use this for initialization  
    void Start () {  
      
    }  
      
    // Update is called once per frame  
    void Update () {  
          
    }  
  
    private void OnTap()  
    {  
        gameObject.GetComponent<MeshRenderer>().material.color = Color.blue;  
    }  
  
    private void OnDoubleTap()  
    {  
        gameObject.GetComponent<MeshRenderer>().material.color = Color.green;  
    }  
}  

3、運行測試

當發生單擊事件

當發生雙擊事件(該處存在一點小問題,雙擊時首先識別到單擊事件,所以會看到先變成藍色,然后變成綠色)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM