LoginSignup
2
1

More than 5 years have passed since last update.

HoloLens チュートリアル (Holograms 101E - 3 Gestures)

Last updated at Posted at 2017-01-17

はじめに

HoloLens チュートリアル (Holograms 101E - 2 Gaze) - Qiita」の続きになります。
Holograms 101E: Introduction with Emulator」の「Chapter 3 - Gestures」を試してみます。

チュートリアル「Gestures」

GazeGestureManager の作成

C# スクリプトの作成

601b.png

  • Project パネルから 「Scripts」 を選択
  • 右クリックを押下し、 「Create」-「C# Script」 を選択
  • 名前を「GazeGestureManager」に変更

602.png

  • 作成した「GazeGestureManager」を、Hierarchy パネルの「OrigamiCollection」オブジェクトにドロップ
  • 「GazeGestureManager」をダブルクリック (-> Visual Studio が起動)

C# スクリプトの編集

604.png

GazeGestureManager.cs
using UnityEngine;
using UnityEngine.VR.WSA.Input;

public class GazeGestureManager : MonoBehaviour
{
    public static GazeGestureManager Instance { get; private set; }

    // Represents the hologram that is currently being gazed at.
    public GameObject FocusedObject { get; private set; }

    GestureRecognizer recognizer;

    // Use this for initialization
    void Start()
    {
        Instance = this;

        // Set up a GestureRecognizer to detect Select gestures.
        recognizer = new GestureRecognizer();
        recognizer.TappedEvent += (source, tapCount, ray) =>
        {
            // Send an OnSelect message to the focused object and its ancestors.
            if (FocusedObject != null)
            {
                FocusedObject.SendMessageUpwards("OnSelect");
            }
        };
        recognizer.StartCapturingGestures();
    }

    // Update is called once per frame
    void Update()
    {
        // Figure out which hologram is focused this frame.
        GameObject oldFocusObject = FocusedObject;

        // Do a raycast into the world based on the user's
        // head position and orientation.
        var headPosition = Camera.main.transform.position;
        var gazeDirection = Camera.main.transform.forward;

        RaycastHit hitInfo;
        if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))
        {
            // If the raycast hit a hologram, use that as the focused object.
            FocusedObject = hitInfo.collider.gameObject;
        }
        else
        {
            // If the raycast did not hit a hologram, clear the focused object.
            FocusedObject = null;
        }

        // If the focused object changed this frame,
        // start detecting fresh gestures again.
        if (FocusedObject != oldFocusObject)
        {
            recognizer.CancelGestures();
            recognizer.StartCapturingGestures();
        }
    }
}

Manager class with API for recognizing user gestures.

Occurs when a Tap gesture is recognized.

Call to begin receiving gesture events on this recognizer. No events will be received until this method is called.

Cancels any pending gesture events. Additionally this will call StopCapturingGestures.

ゲームオブジェクトと親(の親、さらに親 ... )にアタッチされているすべての MonoBehaviour にある methodName と名付けたメソッドを呼び出します


ShpereCommands の作成

C# スクリプトの作成

  • Project パネルから 「Scripts」 を選択
  • 右クリックを押下し、 「Create」-「C# Script」 を選択
  • 名前を「SphereCommands」に変更

603.png

  • 作成した「ShpereCommands」を、Hierarchy パネルの「OrigamiCollection」-「Sphere1」「Spere2」オブジェクトにドロップ
  • 「ShpereCommands」をダブルクリック (-> Visual Studio が起動)

C# スクリプトの編集

SphereCommands.cs
using UnityEngine;

public class SphereCommands : MonoBehaviour
{
    // Called by GazeGestureManager when the user performs a Select gesture
    void OnSelect()
    {
        // If the sphere has no Rigidbody component, add one to enable physics.
        if (!this.GetComponent<Rigidbody>())
        {
            var rigidbody = this.gameObject.AddComponent<Rigidbody>();
            rigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous;
        }
    }
}

Rigidbody を使うと、ゲームオブジェクトを物理特性によって制御する事ができるようになります。

Rigidbody の衝突検出モード。Rigidbody の継続的な衝突判定を設定するためにこれを使用します。

このモードは静的メッシュジオメトリと衝突しているときに検出されるようになります。


Visual Studio のプロジェクト生成

  • 「File」メニューから「Build Settings」を選択
  • 「Build Settings」ウィンドウから「Build」ボタンを押下

ビルド・実行環境設定の確認、実行

  • ソリューション構成 「Release」
  • プラットフォーム 「X86」
  • 実行環境 「HoloLens Emulator」
  • 「デバッグ」メニューから 「デバッグなしで開始」 を選択

エミュレータ操作

605.png

606.png

Gaze のマークを Sphere に合わせて、スペースボタンを押すと、Sphere がポロリと落ちて、台の上を転がって、台からも落ちていきます。

2
1
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
2
1