Hololens⼊门之⼿势识别(单击、双击)
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 rerved.
// Licend under the MIT Licen. See LICENSE in the project root for licen 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 us GazeManager to find the game object.
/// GestureManager then nds a message to that game object.
/// </summary>
[RequireComponent(typeof(GazeManager))]
public partial class GestureManager : Singleton<GestureManager>
{
/// <summary>
/// Key to press in the editor to lect the currently gazed hologram
/// </summary>
public KeyCode EditorSelectKey = KeyCode.Space;
/// <summary>
/// To lect even when a hologram is not being gazed at,
/// t the override focud object.
/// If its null, then the gazed at object will be lected.
/// </summary>
public GameObject OverrideFocudObject
public GameObject OverrideFocudObject
{
get; t;
}
/// <summary>
/// Gets the currently focud object, or null if none.
/// </summary>
public GameObject FocudObject
{
get { return focudObject; }
}
private GestureRecognizer gestureRecognizer;
private GameObject focudObject;
public bool IsNavigating { get; private t; }
public Vector3 NavigationPosition { get; private t; }
void Start()
{
// 创建GestureRecognizer实例
gestureRecognizer = new GestureRecognizer();
// 注册指定的⼿势类型,本例指定单击及双击⼿势类型
gestureRecognizer.SetRecognizableGestures(GestureSettings.Tap
| GestureSettings.DoubleTap);
/
/ 订阅⼿势事件
gestureRecognizer.TappedEvent += GestureRecognizer_TappedEvent;
// 开始⼿势识别
gestureRecognizer.StartCapturingGestures();
}
private void OnTap()
{
if (focudObject != null)
{
小白菜图片focudObject.SendMessage("OnTap");
}
东野圭吾推理系列
}
private void OnDoubleTap()
{
if (focudObject != null)
{
focudObject.SendMessage("OnDoubleTap");
}
}
private void GestureRecognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay) {
if (tapCount == 1)
{
木瓜丸
分数的加减乘除OnTap();
}
el
{
OnDoubleTap();
怎么表白女生}
}
void LateUpdate()
{
GameObject oldFocudObject = focudObject;
if (GazeManager.Instance.Hit &&
if (GazeManager.Instance.Hit &&
OverrideFocudObject == null &&
GazeManager.llider != null)
{
// If gaze hits a hologram, t the focud object to that game object.
// Also if the caller has not decided to override the focud object.
focudObject = GazeManager.llider.gameObject;
}
el
{
// If our gaze doesn't hit a hologram, t the focud object to null or override focud object.
爱之链阅读答案
focudObject = OverrideFocudObject;
}
if (focudObject != oldFocudObject)
{
// If the currently focud object doesn't match the old focud 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.GetMouButtonDown(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 {
// U this for initialization
void Start () {
}
// Update is called once per frame
那次玩的真高兴void Update () {
}
private void OnTap()
{
gameObject.GetComponent<MeshRenderer>().lor = Color.blue;
}
private void OnDoubleTap()
{
gameObject.GetComponent<MeshRenderer>().lor = ;
}
}
3、运⾏测试
当发⽣单击事件
当发⽣双击事件(该处存在⼀点⼩问题,双击时⾸先识别到单击事件,所以会看到先变成蓝⾊,然后变成绿⾊)