hololens 各类开发(手势,凝视...)

归档 | iamYaoJie.Blog git

http://iamyaojie.wang/archives/github

(一)Hololens Unity 开发环境搭建(Mac BOOTCAMP WIN10)

系统要求

  • 64位 Windows 10 除了家庭版的 都支持 ~
  • 64位CPU
  • CPU至少是四核心以上~
  • 至少须要 8 GB 内存, 这个不够本身能够加~
  • GPU
    1. 最低支持DirectX 11.0 或者更高
    2. 最低WDDM 1.2
  • Hardware-assisted virtualization 一个基于主板的虚拟服务(这个就是MAC本上须要手动启动的了~)

接着上面的说,这个在mac本上就没有BIOS主板系统,可是昂贵的Mac确定也是有虚拟化服务的~只不过Mac本不是手动启动,而是每次启动完OSX系统自动启动~ 可是若是 第一次启动的是 bootcamp的Windows 系统 那么 这个 虚拟化是启动不了的。。。这时候有一个解决办法就是先启动OSX系统,再更具目标磁盘重启到bootcamp的Windows系统~ 参考下图~windows

经过这个启动盘重启的Windows虚拟化是 打开的状态~ 以下图网络

虚拟化状态打开后就能够下载安装 HoloLens 的模拟器了 ~ 并且在开发调试中 也必定要把虚拟化打开(切记注意~)app

软件安装 ~

这个按照下面安装下一步下一步就好~ide

Visual Studio 安装
Hololens模拟器安装
Unity 5.5 安装工具

基本环境搭建完成~post

HoloLens模拟器网络链接

在任务栏搜索框输入H,打开hyper-管理器,而后选择交换机管理器,选择外部网络,并选择本身的网卡。若是无效请顺便将windows phone emulator也改了学习

 

 

(二)Hololens Unity 开发入门 之 Hello HoloLens~

学习源于官方文档 微软官文~开发工具

笔记一部分是直接翻译官方文档,部分各人理解不一致的和一些比较浅显的保留英文原文

本文主要记录 HoloLens Unity 开发入门 ~

说在前面的话

Unity 对 VR AR 甚至 未来的 MR 来讲,的确是一个至关不错的开发引擎 ~ (这里就不辩论UE4了~) 通过过去一年的Unity和移动设备的混合开发,总结出了下面的一些结论

便捷开发者对3D的处理

深有感触呀~从最开始接触公司的项目开始的时候,那会用的是Vuforia iOS原生的SDK作的开发,一句一句没有逻辑的 OpenGL 命令行,瞬间让刚刚接触OpenGL的开发者懵逼 ~ Unity做为一个3D引擎,经过底层链接移动端 (OpenGL)或者 HoloLens (D3D)的 显卡接口,向开发者暴露 高度封装的 API,便捷了开发者对图形的处理~

多平台功能性API规范统一

刚刚过去的2016年听说是VR元年,这一年中 ~ 除开国产硬件厂商,著名的海外VR硬件产商也比较多, 大量的硬件产商可能对于消费者来讲 是 一件好事,多了选择的空间 ~ 但是对于开发者来讲 是 一场噩梦呀 ~ 老板的需求永远是但愿一套代码任何硬件都能跑,恩~ 这时候Unity在中间起到一层缓冲的做用。。同一个UnityEngine的方法,同一套代码 能够 在不一样的设备在跑,固然不能百分之百的说 Unity 能很好的兼容各类设备,可是能起到对开发复杂程度 必定 的缓冲做用。(我的感受UnityEngine的API还不够规范,但愿Unity也继续努力吧~)

开发者数量

额 ~ 这个话题官方数据 说 2014年开发者数量 就已经达到全球 330万人~ 额,不过从过去Unity在手游领域来看,数量应该也算是很多了 ~ 就连HoloLens有本身D3D引擎也推荐Unity来开发(至于官文推荐Unity也有多是微软和Unity的一些合做关系吧~) 相对于若是各类AR设备平台的开发者来讲,Unity在应用厂商人才招聘上绝对是一大优点~

总结

其实很容易看出 ~ 上面说到的Unity的所优势都是相对于应用产商来讲,大大的节省了开发成本 ,分为人力成本和技术成本~ 做为硬件厂商的微软 为 广大的应用产商提供了便捷的开发方式,而应用产商开发出大量的基于HoloLens的产品,这时候实现了共赢 ~ 授人玫瑰,手留余香。。。

NOTE:对于HoloLens 来讲,仅仅只用Unity开发HoloLens并不能很好的开发产品。
首先,Unity对于3D图像的处理时至关不错的选择~不过Unity对于界面开发的便捷性无力吐槽~
其次,Unity开发HoloLens是用高度封装好的API,至于实现一些复杂的需求,可能还会涉及核心层面的一些接口的调用 !


HoloToolkit-Unity 集成

使用Unity开发HoloLens的原理和Unity开发移动端游戏同样~ Unity调用了硬件设备提供的API实现一系列功能 ~

下载

HoloToolkit-Unity GitHub下载地址 ~ 点我
首先把它download 吧~ 使用Unity打开,能看到分为两个文件夹,一个是HoloToolKit (这个就是咱们须要的开发工具了),还有一个是 HoloToolkit-Examples 里面都是一些实例~ 能够参考参考 一些 HoloToolKit 方法的调用 ~

压包

选中HoloToolKit 右键 Export Package,再勾选第一个HoloToolKit,,点击Export处处一个Packgage

集成HoloToolKit

新建一个Unity工程 ~ 导入刚刚导处的package 就 OK了~


Camera 配置

1. 设置 Camera 的 Tag 为 MainCamera
2. 设置 Camera 的 position 为 0,0,0
3. 设置 Camera 的 Clear Flags 为 Solid Color
4. 设置 Camera 的 Background RGBA 为 0,0,0,0
5. 设置 Camera Clipping Planes 的 Near 值 为 0.3到0.85之间的一个值~

相机须要改变的配置就是以上这些了~ 查阅官文可得~


Unity工程的配置

1. 选择 File > Build Settings… 更改成 Windows Store 平台
2. 设置 SDK 为Universal 10 
3. 设置 Target device 为 HoloLens 
4. 设置 UWP Build Type 为 D3D 
5. 勾选Debugging 里面的 Unity C# Projects ~ 这样才能导出VS工程 ~

6. 接着点击Player Settings…选择Windows Store 平台,在Other Settings 找到Virtual Reality Supported 而且勾选上 ,点击加号 添加Virtual Reality Devices设备 Windows Holographic ~

7. 选择 Edit > Project Settings > Quality ,点击向下 的 按钮 ~ 选择 Fastest , 最快的~

以上全部的配置完成~ 在场景里面随便丢一个Cube吧~ 导出,,看看效果 ~


Hello HoloLens ~

(三)Hololens Unity 开发之 语音识别

学习源于官方文档 Voice input in Unity

笔记一部分是直接翻译官方文档,部分各人理解不一致的和一些比较浅显的保留英文原文

HoloLens 有三大输入系统,凝视点、手势和声音 ~ 本文主要讲解 语音输入 ~ (测试不支持中文语音输入~)

概述

HoloToolKit Unity 包提供了三种 语音输入的方式 :

  • Phrase Recognition 短语识别
    * KeywordRecognizer   单一关键词识别
    * GrammarRecognizer   语法识别
  • Dictation Recognition 听写识别
    * DictationRecognizer 将声音识别转化为文字

Note: KeywordRecognizer 和 GrammarRecognizer 是主动活跃识别的方式~ 也就是说调用开始识别的方法,那么久处于活跃状态开始识别,而DictationRecognizer只要注册了就就在默默的监听语音输入,一旦监听到关键词~那么久触发回调~

Unity开发打开Microphone权限

下面是官方文档 讲解 如何打开microphone权限,直接上配图~

The Microphone capability must be declared for an app to leverage Voice input.

  1. In the Unity Editor, go to the player settings by navigating to “Edit > Project Settings > Player”
  2. Click on the “Windows Store” tab
  3. In the “Publishing Settings > Capabilities” section, check the Microphone capability

Phrase Recognition 短语识别

To enable your app to listen for specific phrases spoken by the user then take some action, you need to:

  1. Specify which phrases to listen for using a KeywordRecognizer or GrammarRecognizer
  2. Handle the OnPhraseRecognized event and take action corresponding to the phrase recognized

使用短语识别嘞~须要作两个步骤:

  1. 指定须要监听的 短语 或者 关键词
  2. 处理识别到 短语 或者 关键词 以后的事件回调 ~ OnPhraseRecognized

关键词识别 (直接Demo代码~)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Windows.Speech;
using System.Linq;

public class VoiceInputDemo : MonoBehaviour {

    public Material yellow;
    public Material red;
    public Material blue;
    public Material green;

    /// <summary>
    /// 关键词识别对象
    /// </summary>
    private KeywordRecognizer keywordRecognizer;

    /// <summary>
    /// 存放关键词的字典
    /// </summary>
    private Dictionary<string, System.Action> keywords = new Dictionary<string, System.Action>();
    // Use this for initialization
    void Start () {

        // 向字典中添加关键词,key为关键词, vallue为一个匿名action
        keywords.Add("yellow", () =>
        {
            Debug.Log("听到了 yellow");
            transform.GetComponent<MeshRenderer>().material = yellow;
        });

        keywords.Add("red", () =>
        {
            Debug.Log("听到了 red");
            transform.GetComponent<MeshRenderer>().material = red;
        });

        keywords.Add("green", () =>
        {
            Debug.Log("听到了 green");
            transform.GetComponent<MeshRenderer>().material = green;
        });

        keywords.Add("blue", () =>
        {
            Debug.Log("听到了 blue");
            transform.GetComponent<MeshRenderer>().material = blue;
        });

        // 初始化关键词识别对象
        keywordRecognizer = new KeywordRecognizer(keywords.Keys.ToArray());

        // 添加关键词代理事件
        keywordRecognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;

        // 注意: 这方法必定要写,开始执行监听
        keywordRecognizer.Start();
    }

    private void KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
    {

        System.Action keywordAction;
        // if the keyword recognized is in our dictionary, call that Action.
        // 若是关键字在咱们的字典中被识别,调用该action。
        if (keywords.TryGetValue(args.text, out keywordAction))
        {
            Debug.Log("听到了,进入了事件方法  关键词语 : " + args.text.ToString());

           // 执行该action
            keywordAction.Invoke();
        }
    }

    // Update is called once per frame
    void Update () {

    }
}

二、 语法识别 GrammarRecognizer

按照官方文档上来讲的 我得 建立一个 SRGS 的XML文件放在 StreamingAssets 文件夹下~不过我没有作到英文语法输入的需求 ~ 感兴趣的点击 https://msdn.microsoft.com/en-us/library/hh378349(v=office.14).aspx 本身查看官方文段对SRGS的讲解~

下面贴的一段官方文档的代码
Once you have your SRGS grammar, and it is in your project in a StreamingAssets folder:

<PROJECT_ROOT>/Assets/StreamingAssets/SRGS/myGrammar.xml

Create a GrammarRecognizer and pass it the path to your SRGS file:

private GrammarRecognizer grammarRecognizer;
grammarRecognizer = new GrammarRecognizer(Application.streamingDataPath + "/SRGS/myGrammar.xml");

Now register for the OnPhraseRecognized event

grammarRecognizer.OnPhraseRecognized += grammarRecognizer_OnPhraseRecognized;

You will get a callback containing information specified in your SRGS grammar which you can handle appropriately. Most of the important information will be provided in the semanticMeanings array.

private void Grammar_OnPhraseRecognized(PhraseRecognizedEventArgs args)
{
    SemanticMeaning[] meanings = args.semanticMeanings;
    // do something
}

Finally, start recognizing!

grammarRecognizer.Start();

听写

概述

DictationRecognizer 使用这个对象能够识别语音输入转化为文本,使用这个对象有三个步骤~

  1. 建立一个DictationRecognizer对象
  2. 注册Dictation 事件
  3. 开始识别听写

开启网络客户端权限

The “Internet Client” capability, in addition to the “Microphone” capability mentioned above, must be declared for an app to leverage dictation.

  1. In the Unity Editor, go to the player settings by navigating to “Edit > Project Settings > Player” page
  2. Click on the “Windows Store” tab
  3. In the “Publishing Settings > Capabilities” section, check the InternetClient capability

直接上Unity的图吧~

Demo代码示例~

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Windows.Speech;

public class VoiceDictationDemo : MonoBehaviour
{

    private DictationRecognizer dictationRecognizer;

    // Use this for initialization
    void Start()
    {

        // 定义一个听写对象
        dictationRecognizer = new DictationRecognizer();

        // 注册一个 结果回调 事件
        dictationRecognizer.DictationResult += DictationRecognizer_DictationResult;
        // 注册一个 完成 事件
        dictationRecognizer.DictationComplete += DictationRecognizer_DictationComplete;
        // 注册一个 错误 事件
        dictationRecognizer.DictationError += DictationRecognizer_DictationError;
        // 注册一个 识别语句 的 事件
        dictationRecognizer.DictationHypothesis += DictationRecognizer_DictationHypothesis;

        dictationRecognizer.Start();
    }

    private void DictationRecognizer_DictationHypothesis(string text)
    {
        Debug.Log("进入了Hypothesis 的 事件 回调 ~ " + text);
        dictationRecognizer.Start();
    }

    private void DictationRecognizer_DictationError(string error, int hresult)
    {
        Debug.Log("进入了Error 的 事件 回调 ~ " + error + " 状态码 " + hresult);
        dictationRecognizer.Start();
    }

    private void DictationRecognizer_DictationComplete(DictationCompletionCause cause)
    {

        Debug.Log("进入了Complete 的 事件 回调 ~ " + cause);
        dictationRecognizer.Start();
    }

    private void DictationRecognizer_DictationResult(string text, ConfidenceLevel confidence)
    {
        Debug.Log("进入了Result 的 事件 回调 ~ " + text + " 枚举 " + confidence);
        dictationRecognizer.Start();
    }

    void OnDestroy()
    {
        // 销毁事件
        dictationRecognizer.DictationResult -= DictationRecognizer_DictationResult;
        dictationRecognizer.DictationComplete -= DictationRecognizer_DictationComplete;
        dictationRecognizer.DictationHypothesis -= DictationRecognizer_DictationHypothesis;
        dictationRecognizer.DictationError -= DictationRecognizer_DictationError;
        dictationRecognizer.Dispose();
    }

}

用有道 里面 的英语短视频 作了下测试~ 几乎能达到百分之九十八 以上的 识别率。。感叹微软作的挺不错的~

同时使用 语音识别 和 听写 (文档翻译)

If you want to use both phrase recognition and dictation in your app, you’ll need to fully shut one down before you can start the other. If you have multiple KeywordRecognizers running, you can shut them all down at once with:
若是你想同时使用 语音识别 和 听写识别,那么你必须关闭一个再启动另一个~ 若是你有多个语音识别的对象KeywordRecognizers,那么你能够经过下面的方法把他们所有关闭~

PhraseRecognitionSystem.Shutdown();

In order to restore all recognizers to their previous state, after the DictationRecognizer has stopped, you can call:
固然,你也能够恢复关闭前的全部状态,当在你的听写识别结束的时候,你能够调用下面的方法恢复以前的语音识别~

PhraseRecognitionSystem.Restart();

You could also just start a KeywordRecognizer, which will restart the PhraseRecognitionSystem as well.
固然,你也能够只启动一个KeywordRecognizer语音识别对象~一样的也是用PhraseRecognitionSystem来控制其暂停或者恢复~

 

(四)Hololens Unity 开发之 凝视系统

学习源于官方文档 Gaze in Unity

笔记一部分是直接翻译官方文档,部分各人理解不一致的和一些比较浅显的保留英文原文

HoloLens 有三大输入系统,凝视点、手势和声音 ~ 本文主要记录凝视系统的学习笔记 ~

概述

Gaze is the first input mechanism on HoloLens. It’s the first “G” in Gaze, Gesture, and Voice input models on HoloLens. However, there is no explicit API or Component exposing Gaze in Unity.

凝视系统是HoloLens的第一输入机制~(其实我以为是借助了cardboard的交互模式),官文上有这么一句话 However, there is no explicit API or Component exposing Gaze in Unity. 凝视系统嘞~ 没有统一的API,因此只须要本身去实现就好~ 不是太复杂,下面是凝视系统的原理。


凝视系统原理

原理很简单:射线碰撞检测 ,unity开发者都比较熟悉了~下面是官文中对射线碰撞的描述~再也不翻译

Conceptually, Gaze is implemented by projecting a ray from the user’s head where the HoloLens is, in the forward direction they are facing and determining what that ray collides with. In Unity, the user’s head position and direction are exposed through the Unity Main Camera, specifically UnityEngine.Camera.main.transform.forward and UnityEngine.Camera.main.transform.position.

Calling Physics.RayCast results in a RaycastHit structure which contains information about the collision including the 3D point where collision occurred and the other GameObject the gaze ray collided with.

上Demo代码~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class GazeDemo : MonoBehaviour {
 
// Use this for initialization
void Start () {
 
}
 
// Update is called once per frame
void Update () {
 
RaycastHit hitinfo;
if (Physics.Raycast(
Camera.main.transform.position,
Camera.main.transform.forward,
out hitinfo,
20.0f,
Physics.DefaultRaycastLayers)
)
{
Debug.Log( "检测到了 物体 ~");
 
}
}
}

射线检测碰撞 涉及到的一些具体的参数~请自行查阅unity文档~ 案例很简单,下图是模拟器的实际效果

上图能够看出,模拟器的凝视点为屏幕中心点~ 而上面的官文提升过,设备上的凝视点为用户的眼睛 以及 透过全息透镜 肯定的直线 来肯定的凝视点~ 两点控制一条直线~ 眼睛 – 全息透镜中心点~


Visualizing Gaze 可视化的凝视点~

这个比较容易理解了,相似cardboard中的焦点,固然,也能够本身实现,不过HoloLens在HoloToolkit-Unity的工具中已经提供了一个类 GazeManager.cs ,用于实现可视化焦点~ 并且用起来也挺方便的~

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HoloToolkit.Unity.InputModule;
 
public class VisualizingGazeDemo : MonoBehaviour {
 
private GazeManager gazeManager;
 
// Use this for initialization
void Start () {
 
gazeManager = GazeManager.Instance;
gazeManager.FocusedObjectChanged += OnFocusedObjectChanged;
 
Debug.Log( "初始化完成~");
 
}
 
private void OnFocusedObjectChanged(GameObject previousObject, GameObject newObject)
{
Debug.Log( "检测到 物体 的 名字 ~ " + newObject.name);
}
 
// Update is called once per frame
void Update () {
 
}
}

注意了~ GazeManager 是个mono单例类~因此必定要把它先挂到场景里面~

 

(五)Hololens Unity 开发之 手势识别

学习源于官方文档 Gestures in Unity

笔记一部分是直接翻译官方文档,部分各人理解不一致的和一些比较浅显的保留英文原文

HoloLens 有三大输入系统,凝视点、手势和声音 ~ 本文主要记录手势识别的学习笔记 ~

概述

Gestures are the second “G” in Gaze, Gesture, and Voice. Unity provides both low level access (raw position and velocity information) and a high level gesture recognizer that exposes more complex gesture events (for example: tap, double tap, hold, manipulation and navigation).  

手势识别是Hololens 三大输入系统 Gaze焦点、Gesture手势 和 Voice声音 中的第二个G~(这句翻译的好像有点儿生硬了~)同时在Unity层面也提供了底层的API,例如原始位置速度位移信息等~ 固然,也提供了高维度封装好的供开发者调用的高层API~例如:手势单击,双击,长按,多点点击以及导航栏…(彻底按照开发者的思惟翻译的~)

官文能够得知~ HoloLens提供了高层次的和核心的API供开发者调用~

  • Gesture Input
  • Interaction Input

Gesture Input(高层次分装的API)

Namespace: UnityEngine.VR.WSA.Input

Types: GestureRecognizer, GestureSettings, InteractionSourceKind

These high level gestures are generated by input sources. Each Gesture event provides the SourceKind for the input as well as the targeting head ray at the time of the event. Some events provide additional context specific information.

这是一些高层次的手势源,每一个手势输入都对应了一个事件event~ 而每一个事件都提供了SourceKind 手势输入员的类别 以及 在 头部感应器触发事件的事件~ 并且一些事件来提供了额外的一些信息~

高层的封装表明着简洁,方便,快捷~因此咱们只要经过下面几个步骤咱们就能实现手势识别了~

  1. 建立一个手势识别对象 Gesture Recognizer
  2. 设置手势监听的类型
  3. 注册事件
  4. 开启手势识别的功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.VR.WSA.Input;
 
public class GestureDemo : MonoBehaviour {
 
private GestureRecognizer recognizer;
 
// Use this for initialization
void Start () {
Debug.Log( "初始化开始~");
// 建立手势识别对象
recognizer = new GestureRecognizer();
// 设置手势识别的类型
recognizer.SetRecognizableGestures(GestureSettings.Tap | GestureSettings.Hold | GestureSettings.DoubleTap);
// 添加手势识别的事件
recognizer.TappedEvent += Recognizer_TappedEvent;
recognizer.HoldStartedEvent += Recognizer_HoldStartedEvent;
recognizer.HoldCompletedEvent += Recognizer_HoldCompletedEvent;
recognizer.HoldCanceledEvent += Recognizer_HoldCanceledEvent;
// 开启手势识别
recognizer.StartCapturingGestures();
Debug.Log( "初始化完成~");
}
 
private void Recognizer_HoldCanceledEvent(InteractionSourceKind source, Ray headRay)
{
Debug.Log( "Recognizer_HoldCanceledEvent 枚举类型应该为1 ~ " + source);
}
 
private void Recognizer_HoldCompletedEvent(InteractionSourceKind source, Ray headRay)
{
Debug.Log( "Recognizer_HoldCompletedEvent 枚举类型应该为1 ~ " + source);
}
 
private void Recognizer_HoldStartedEvent(InteractionSourceKind source, Ray headRay)
{
Debug.Log( "Recognizer_HoldStartedEvent 枚举类型应该为1 ~ " + source);
}
 
private void Recognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay)
{
Debug.Log( "Recognizer_TappedEvent 枚举类型应该为1 ~ " + source + " 点击的次数 " + tapCount);
}
 
void OnDestroy()
{
// 销毁注册的事件
recognizer.TappedEvent -= Recognizer_TappedEvent;
recognizer.HoldStartedEvent -= Recognizer_HoldStartedEvent;
recognizer.HoldCompletedEvent -= Recognizer_HoldCompletedEvent;
recognizer.HoldCanceledEvent -= Recognizer_HoldCanceledEvent;
}
}

一切尽在代码中~


Interaction Input(核心API)

Namespace: UnityEngine.VR.WSA.Input

Types: InteractionManager, InteractionSourceState, InteractionSource, InteractionSourceProperties, InteractionSourceKind, InteractionSourceLocation

API介绍

手势交互核心层的API调用分厂简单,三步搞定~

  1. 注册手势交互的事件
  2. 接收事件回调
  3. 移除手势交互的事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.VR.WSA.Input;
 
public class InteractionDemo : MonoBehaviour {
 
// Use this for initialization
void Start () {
// 点击某个控件的事件
InteractionManager.SourcePressed += InteractionManager_SourcePressed;
// 点击某个控件放开的事件
InteractionManager.SourceReleased += InteractionManager_SourceReleased;
// 选中某个控件的事件
InteractionManager.SourceDetected += InteractionManager_SourceDetected;
// 选中后再不选中的事件
InteractionManager.SourceLost += InteractionManager_SourceLost;
// 更新某个控件的事件
InteractionManager.SourceUpdated += InteractionManager_SourceUpdated;
}
 
private void InteractionManager_SourceUpdated(InteractionSourceState state)
{
throw new System.NotImplementedException();
}
 
private void InteractionManager_SourceReleased(InteractionSourceState state)
{
throw new System.NotImplementedException();
}
 
private void InteractionManager_SourceLost(InteractionSourceState state)
{
throw new System.NotImplementedException();
}
 
private void InteractionManager_SourceDetected(InteractionSourceState state)
{
throw new System.NotImplementedException();
}
 
private void InteractionManager_SourcePressed(InteractionSourceState state)
{
throw new System.NotImplementedException();
}
 
void OnDestroy()
{
// 移除各类手势的事件
InteractionManager.SourceDetected -= InteractionManager_SourceDetected;
InteractionManager.SourceUpdated -= InteractionManager_SourceUpdated;
InteractionManager.SourceLost -= InteractionManager_SourceLost;
InteractionManager.SourcePressed -= InteractionManager_SourcePressed;
InteractionManager.SourceReleased -= InteractionManager_SourceReleased;
}
}

InteractionSourceState 结构体

源码~

1
2
3
4
5
6
7
8
9
10
11
namespace UnityEngine.VR.WSA.Input
{
[ RequiredByNativeCodeAttribute]
public struct InteractionSourceState
{
public Ray headRay { get; }
public bool pressed { get; }
public InteractionSourceProperties properties { get; }
public InteractionSource source { get; }
}
}
  • headRay 这个变量能够肯定该事件发生时用户头部所处的位置信息
  • pressed 是否处于点击状态
  • InteractionSourceProperties 能够经过InteractionSourceProperties得到输入源的位置、速度以及输入源的时间点
  • InteractionSource 枚举类型,能够是不一样的输入类型,包括手势 声音 控制器 以及 其余等等~
 
坚持原创技术分享,您的支持将鼓励我继续创做!
相关文章
相关标签/搜索