Hololens入门之手势识别(单击、双击)
本文使用手势识别实现识别单击及双击手势的功能,当单击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;
}
}
当发生单击事件
当发生双击事件(该处存在一点小问题,双击时首先识别到单击事件,所以会看到先变成蓝色,然后变成绿色)