環境
- 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」が追加される。
設定したい項目を選ぶと以下のような画面が出るので、必要な箇所にチェックを入れ、Applyをクリック。
今回は初期の状態で実行。
ビルド設定
Build Settingsから「Universal Windows Platform」を選択し、「Switch Platform」をクリック。
次にPlayer Settingsを開きOther Sttings(Unity2017.2の場合はXR Settings)の「Virtual Reality Supported」にチェックを入れ、「Windows Holographic」を選択します。
カーソル関係
カーソルの追加
参考:HoloToolKitのAirTapに関するメモ(AirTapイベントの取得)
Holotooolkit > Input > Prefabs
からInputManagerをHierarchyに追加。
Holotooolkit > Input > Prefabs > Cursor
から好きなCursorのprefabをHierarchyに追加。
カーソルをオブジェクトにかぶせると形が変わる。
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を作成し、以下のように設定
青色のCubeが視線を合わせると緑色に変わる。
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にアタッチし、以下のように設定。
タップすると赤と緑で色が変わる。
ボタンの追加
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;
}
}
}
ボタンイベントに作成したOnClickedを設定する。
これでボタンをAirTapする度にCubeの色が変わる