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 枚舉類型,能夠是不一樣的輸入類型,包括手勢 聲音 控制器 以及 其餘等等~
 
堅持原創技術分享,您的支持將鼓勵我繼續創做!
相關文章
相關標籤/搜索