UnityでHTC Vive対応のコンテンツを開発 その1
はじめに
Unityで、HTC Vive対応のコンテンツを開発する方法について、
メモ程度に書いていこうと思います。
使用したもの
HTC Vive
Unity 5.5.3
SteamVR Plugin 1.2.1
参考にしたページ
Unity+HTC Vive開発メモ - フレームシンセシス
準備
- Unityをインストール
- 新規プロジェクトを作成する。
-
SteamVR PluginをAsset Storeからダウンロードし、インポートする。
推奨設定に変更させようとするウィンドウが出るので、左下の「Accept All」をクリックする。
- 自動追加されるカメラを削除し、SteamVR/Prefabs/[CameraRig]プレハブをシーン内に設置する。
- 同様に、[Status]プレハブ、[SteamVR]プレハブも設置する。
これでシーンを再生すれば、HTC Viveと接続されてコンテンツがHMDで再生されるはずです。
コントローラの取得
コントローラの位置
[CameraRig]の下に、Controller (left)とController (right)というゲームオブジェクトがあり、
このtransformを利用することで、位置が取得できます。
コントローラの入力
Unity+HTC Vive開発メモ - フレームシンセシス
こちらのサイトには以下のようなサンプルプログラムが掲載されています。
using UnityEngine;
public class ControllerExample : MonoBehaviour
{
void Update()
{
SteamVR_TrackedObject trackedObject = GetComponent<SteamVR_TrackedObject>();
var device = SteamVR_Controller.Input((int) trackedObject.index);
if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger)) {
Debug.Log("トリガーを浅く引いた");
}
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)) {
Debug.Log("トリガーを深く引いた");
}
if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)) {
Debug.Log("トリガーを離した");
}
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad)) {
Debug.Log("タッチパッドをクリックした");
}
if (device.GetPress(SteamVR_Controller.ButtonMask.Touchpad)) {
Debug.Log("タッチパッドをクリックしている");
}
if (device.GetPressUp(SteamVR_Controller.ButtonMask.Touchpad)) {
Debug.Log("タッチパッドをクリックして離した");
}
if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Touchpad)) {
Debug.Log("タッチパッドに触った");
}
if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Touchpad)) {
Debug.Log("タッチパッドを離した");
}
if (device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu)) {
Debug.Log("メニューボタンをクリックした");
}
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Grip)) {
Debug.Log("グリップボタンをクリックした");
}
if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger)) {
//Debug.Log("トリガーを浅く引いている");
}
if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger)) {
//Debug.Log("トリガーを深く引いている");
}
if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad)) {
//Debug.Log("タッチパッドに触っている");
}
}
}
このようなスクリプトを書いて、Controller (left)とController (right)にアタッチすれば、入力が取得できるようです。
個人的には、deviceを取得する部分のコードを書くのがめんどくさいので、
以下のような拡張メソッドを作って使用しています。(SteamVR_TrackedObjectの代わりにSteamVR_TrackedControllerを使用しています。)
public static SteamVR_Controller.Device GetDevice(this SteamVR_TrackedController self)
{
return SteamVR_Controller.Input((int)self.controllerIndex);
}
また、クリックなどの入力は、イベントを使用して以下のようにも書くことが出来ます。
private void SetControllerEventExample()
{
trackedController.TriggerClicked += new ClickedEventHandler(DoTriggerClicked);
trackedController.TriggerUnclicked += new ClickedEventHandler(DoTriggerUnClicked);
trackedController.MenuButtonClicked += new ClickedEventHandler(DoMenuButtonClicked);
}
public void DoTriggerClicked(object sender, ClickedEventArgs e)
{
Debug.Log("トリガーを引いた");
}
public void DoTriggerUnClicked(object sender, ClickedEventArgs e)
{
Debug.Log("トリガーを離した");
}
public void DoMenuButtonClicked(object sender, ClickedEventArgs e)
{
Debug.Log("メニューボタンをクリックした");
}
とりあえず今回はこのへんで
次回はコントローラからRayを出す話とかですかね