8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Hololens】Mixed Reality Toolkitを使う【AirTap】

Last updated at Posted at 2017-12-07

環境

  • Windows 10
  • Unity 2017.1.0p4
    2017.2でビルドがうまく行かなかったため、今回は2017.1を使用しております。

準備

ダウンロード

Unity向けMixedRealityToolKitダウンロード先:
https://github.com/Microsoft/MixedRealityToolkit-Unity/releases

インポート

Assets > Import Package > Custom Package
ダウンロードしたパッケージをインポートする

Unityの設定

自動設定

MixedRealityToolkitをインポートするとUnityのメニューに「Mixed Reality Toolkit」が追加される。

20171207-165054.jpg

設定したい項目を選ぶと以下のような画面が出るので、必要な箇所にチェックを入れ、Applyをクリック。
今回は初期の状態で実行。

20171207-165235.jpg

ビルド設定

Build Settingsから「Universal Windows Platform」を選択し、「Switch Platform」をクリック。
次にPlayer Settingsを開きOther Sttings(Unity2017.2の場合はXR Settings)の「Virtual Reality Supported」にチェックを入れ、「Windows Holographic」を選択します。

20171207-165756.jpg

カーソル関係

カーソルの追加

参考:HoloToolKitのAirTapに関するメモ(AirTapイベントの取得)
Holotooolkit > Input > Prefabs
からInputManagerをHierarchyに追加。
Holotooolkit > Input > Prefabs > Cursor
から好きなCursorのprefabをHierarchyに追加。

20171206-201217.jpg

カーソルをオブジェクトにかぶせると形が変わる。

InputManager内にあるEditorHandsInputは使用しないので非アクティブにしておく(画面に両手のカーソルが表示されていたいので)

視線イベント

視線のイベントはHoloToolkit.Unity.InputModuleのIFocusableを継承すると

OnFocusEnter()
OnFocusExit()

でが呼ばれる。
以下サンプルコード

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HoloToolkit.Unity.InputModule;

public class CubeGazefocus : MonoBehaviour, IFocusable {
    public Material focusOn;
    public Material focusOut;

    public void OnFocusEnter()
    {
        GetComponent<Renderer>().material = focusOn;
    }

    public void OnFocusExit()
    {
        GetComponent<Renderer>().material = focusOut;
    }
}

HierarchyにCubeを追加し、このコードをアタッチ。
BlueとGreenのMaterialを作成し、以下のように設定

20171207-100607.jpg

青色のCubeが視線を合わせると緑色に変わる。

20171207-95755.jpg

20171207-95816.jpg

AirTapイベントの追加

AirTapのイベントはHoloToolkit.Unity.InputModuleのIInputClickHandlerを継承すると

OnInputClicked(InputClickEventData eventData){}

が呼ばれる。
以下サンプルコード

using UnityEngine;
using System.Collections;
using HoloToolkit.Unity.InputModule;

public class CubeCommand : MonoBehaviour, IInputClickHandler
{
    public Material first;
    public Material second;

    public void OnInputClicked(InputClickedEventData eventData)
    {
        var material = GetComponent<Renderer>().material;
        GetComponent<Renderer>().material = (material.color == first.color) ? second : first;
    }
}

このコードを先程のCubeにアタッチし、以下のように設定。

20171207-101302.jpg

タップすると赤と緑で色が変わる。

20171207-101438.jpg

20171207-101512.jpg

ボタンの追加

UnityのUI Buttonがそのまま使用できる。
Create > UI > Button
よりボタンを追加。
CanvasのRendarModeを「World Space」に変更。
ボタンを見やすい位置へ移動する。
以下のサンプルコードをCubeにアタッチする。
※AirTapイベントで作成したスクリプトは不要です。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ClickButton : MonoBehaviour {
    public Material click1;
    public Material click2;
    private bool isTouchOn = false;

    public void OnClicked()
    {
        if (isTouchOn)
        {
            isTouchOn = false;
            GetComponent<Renderer>().material = click1;
        }
        else
        {
            isTouchOn = true;
            GetComponent<Renderer>().material = click2;
        }
    }
}

20171207-143342.jpg

ボタンイベントに作成したOnClickedを設定する。

20171207-143643.jpg

これでボタンをAirTapする度にCubeの色が変わる

20171207-144755.jpg

20171207-144827.jpg

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?