本文使用手势识别实现识别单击及双击手势的功能,当单击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 == )
{
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() || 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、运行测试

当发生单击事件

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

Hololens开发笔记之Gesture手势识别(单击,双击)的更多相关文章

  1. Hololens开发笔记之Gesture手势识别(基本介绍)

    手势识别是HoloLens交互的重要输入方法之一.HoloLens提供了底层API和高层API,可以满足不同的手势定制需求.底层API能够获取手的位置和速度信息,高层API则借助手势识别器来识别预设的 ...

  2. Hololens开发笔记之Gesture手势识别(Manipulation手势控制物体旋转)

    Manipulation gesture:保持点击手势,在3D世界中绝对运动 当你想要全息图像1:1响应用户手部移动时,操纵手势能被用于移动.缩放或旋转全息图像.如此的一个用处是使得用户可以在世界中绘 ...

  3. Hololens开发笔记之Gesture手势识别(Manipulation手势控制物体平移)

    Manipulation gesture:保持点击手势,在3D世界中绝对运动 当你想要全息图像1:1响应用户手部移动时,操纵手势能被用于移动.缩放或旋转全息图像.如此的一个用处是使得用户可以在世界中绘 ...

  4. Hololens开发笔记之Gesture手势识别(手势检测反馈)

    本文实现当使用者手出现在Hololens视野范围内时,跟踪手并给出反馈的效果. 1.在Manager上添加HandsManager脚本组件,用于追踪识别手 HandsManager.cs如下(直接使用 ...

  5. HoloLens开发笔记之Gesture input手势输入

    手势是HoloLens三个首要输入形式之一.一旦你使用凝视定位了一个全息图像,手势允许你与它交互.手势输入允许你使用手或者点击器原生地与全息图像交互. 手势之外,你也可以在应用中使用语音输入来交互. ...

  6. Hololens开发笔记之使用Unity开发一个简单的应用

    一.Hololens概述 Hololens有以下特性 1.空间映射借助微软特殊定制的全息处理单元(HPU),HoloLens 实现了对周边环境的快速扫描和空间匹配.这保证了 HoloLens能够准确地 ...

  7. Android 手势识别——单击/双击

    为什么需要手势识别? 手势对于我们的app有很多的地方都在使用,比如右滑关闭界面等.手势控制分为触发动作(Touch Mechanics,用户手指在屏幕上如何动作)和触发行为(Touch Activi ...

  8. Hololens开发笔记之连接PC实现资源共享

    官网原文介绍:https://developer.microsoft.com/en-us/windows/holographic/using_the_windows_device_portal Hol ...

  9. Hololens开发笔记:UDP接收数据

    Hololens的应用需要与其他设备通信的时候,UDP是比较方便的一种方式,Unity3d 2017.3 C#开发的时候可以用Windows.Networking.Sockets.DatagramSo ...

随机推荐

  1. 读《程序员的SQL金典》[2]--函数

    一.数学函数 1.RAND SELECT RAND () ---0.302870228294199 取0-1之间的随机小数. 2.小数取整 CEILINT(data)舍掉小数部分并向上取整. FLOO ...

  2. eclipse 本地项目提交到远程库以及从远程库中添加项目 ---git

    本地项目提交到远程库 1.右击项目->team->share project 2.选择本地库 从远处库中的项目拉到本地 1.右击项目->import项目

  3. centos7.0 64位系统 安装PHP 支持 nginx

    1  安装PHP所需要的扩展 yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel curl cur ...

  4. Java:Collection List Set

    Java:集合 常见集合:List Set List 特点:元素是有序的,而且元素可以重复,因为该集合体系有索引. 常见的三个子类:ArrayList.LinkedList.Verctor List集 ...

  5. ios中autolayout

    IOS 6 自动布局 入门-1  Matthijs Hollemans on September 29, 2012 Tweet 这篇文章还可以在这里找到 英语, 韩语, 土耳其语 If you're ...

  6. c#调用存储过程

    存储过程(Stored Procedure)即用来存储数据表操作的一个过程,是把对数据表操作的方法存储到一起的一个对象,是存储在数据库中. 优点:1.降低网络传输数据量:通过存储过程的名称和参数传递即 ...

  7. IT公司100题-32-交换元素,使数组差最小

    问题描述: 有两个整数序列a, b,大小都为n, 序列元素的值任意整数,无序. 要求:通过交换a, b 中的元素,使得sum(a)-sum(b),差最小. 例如: var a=[80, 40, 60, ...

  8. UIView及其子类

    一.UI概述 UI(User Interface):用户界⾯,用户能看到的各种各样的⻚面元素. iOS App = 各种各样的UI控件 + 业务逻辑和算法 二.UIView 在手机上显示的内容都是UI ...

  9. 记录一些容易忘记的属性 -- UITabBarController

    UIViewController中的  @property(nonatomic,copy) NSString *title;  // Localized title for use by a pare ...

  10. 安卓手机上的python运行环境-qpython

    qpython是一个能让安卓手机运行和编写python的APP,到网上下载APK安装或者在GOOGLE PLAY搜索安装即可. 安装之后你可以你手机跑自己的python程序. qpython有两个大版 ...