当前位置:   article > 正文

Hololens入门之手势识别(单击、双击)_hololens手势识别

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事件时,判断是单击还是双击事件

  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License. See LICENSE in the project root for license information.
  3. using System;
  4. using UnityEngine;
  5. using UnityEngine.VR.WSA.Input;
  6. namespace HoloToolkit.Unity
  7. {
  8. /// <summary>
  9. /// GestureManager creates a gesture recognizer and signs up for a tap gesture.
  10. /// When a tap gesture is detected, GestureManager uses GazeManager to find the game object.
  11. /// GestureManager then sends a message to that game object.
  12. /// </summary>
  13. [RequireComponent(typeof(GazeManager))]
  14. public partial class GestureManager : Singleton<GestureManager>
  15. {
  16. /// <summary>
  17. /// Key to press in the editor to select the currently gazed hologram
  18. /// </summary>
  19. public KeyCode EditorSelectKey = KeyCode.Space;
  20. /// <summary>
  21. /// To select even when a hologram is not being gazed at,
  22. /// set the override focused object.
  23. /// If its null, then the gazed at object will be selected.
  24. /// </summary>
  25. public GameObject OverrideFocusedObject
  26. {
  27. get; set;
  28. }
  29. /// <summary>
  30. /// Gets the currently focused object, or null if none.
  31. /// </summary>
  32. public GameObject FocusedObject
  33. {
  34. get { return focusedObject; }
  35. }
  36. private GestureRecognizer gestureRecognizer;
  37. private GameObject focusedObject;
  38. public bool IsNavigating { get; private set; }
  39. public Vector3 NavigationPosition { get; private set; }
  40. void Start()
  41. {
  42. // 创建GestureRecognizer实例
  43. gestureRecognizer = new GestureRecognizer();
  44. // 注册指定的手势类型,本例指定单击及双击手势类型
  45. gestureRecognizer.SetRecognizableGestures(GestureSettings.Tap
  46. | GestureSettings.DoubleTap);
  47. // 订阅手势事件
  48. gestureRecognizer.TappedEvent += GestureRecognizer_TappedEvent;
  49. // 开始手势识别
  50. gestureRecognizer.StartCapturingGestures();
  51. }
  52. private void OnTap()
  53. {
  54. if (focusedObject != null)
  55. {
  56. focusedObject.SendMessage("OnTap");
  57. }
  58. }
  59. private void OnDoubleTap()
  60. {
  61. if (focusedObject != null)
  62. {
  63. focusedObject.SendMessage("OnDoubleTap");
  64. }
  65. }
  66. private void GestureRecognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay)
  67. {
  68. if (tapCount == 1)
  69. {
  70. OnTap();
  71. }
  72. else
  73. {
  74. OnDoubleTap();
  75. }
  76. }
  77. void LateUpdate()
  78. {
  79. GameObject oldFocusedObject = focusedObject;
  80. if (GazeManager.Instance.Hit &&
  81. OverrideFocusedObject == null &&
  82. GazeManager.Instance.HitInfo.collider != null)
  83. {
  84. // If gaze hits a hologram, set the focused object to that game object.
  85. // Also if the caller has not decided to override the focused object.
  86. focusedObject = GazeManager.Instance.HitInfo.collider.gameObject;
  87. }
  88. else
  89. {
  90. // If our gaze doesn't hit a hologram, set the focused object to null or override focused object.
  91. focusedObject = OverrideFocusedObject;
  92. }
  93. if (focusedObject != oldFocusedObject)
  94. {
  95. // If the currently focused object doesn't match the old focused object, cancel the current gesture.
  96. // Start looking for new gestures. This is to prevent applying gestures from one hologram to another.
  97. gestureRecognizer.CancelGestures();
  98. gestureRecognizer.StartCapturingGestures();
  99. }
  100. #if UNITY_EDITOR
  101. if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(EditorSelectKey))
  102. {
  103. OnTap();
  104. }
  105. #endif
  106. }
  107. void OnDestroy()
  108. {
  109. gestureRecognizer.StopCapturingGestures();
  110. gestureRecognizer.TappedEvent -= GestureRecognizer_TappedEvent;
  111. }
  112. }
  113. }

2、在Cube上添加处理脚本CubeScript.cs


CubeScript脚本如下,定义两个方法,OnTap将Cube的颜色设置为蓝色, OnDoubleTap将Cube的颜色设置为绿色

  1. using UnityEngine;
  2. using System.Collections;
  3. public class CubeScript : MonoBehaviour {
  4. // Use this for initialization
  5. void Start () {
  6. }
  7. // Update is called once per frame
  8. void Update () {
  9. }
  10. private void OnTap()
  11. {
  12. gameObject.GetComponent<MeshRenderer>().material.color = Color.blue;
  13. }
  14. private void OnDoubleTap()
  15. {
  16. gameObject.GetComponent<MeshRenderer>().material.color = Color.green;
  17. }
  18. }


3、运行测试

当发生单击事件


当发生双击事件(该处存在一点小问题,双击时首先识别到单击事件,所以会看到先变成蓝色,然后变成绿色)



声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号