LoginSignup
2
4

More than 5 years have passed since last update.

HoloLens 注視入力ボタンをつくーる

Last updated at Posted at 2018-12-31

大晦日ハッカソン2018の進捗です。

AI-EXPO2018で展示したときに作った注視入力ボタンについてまとめました。
下記動画のマニュアルの左右にある「次へ」もしくは「戻る」ボタンに視線を合わせると、ゲージがたまりページをめくる機能です。


もちろん、実機なしでも確認できます!

開発環境

  • HoloLens RS5
  • Visual Studio 2017 (15.9.2)
  • Unity 2017.4.11f1
  • HoloToolkit-Unity-2017.4.3.0.unitypackage
  • HoloToolkit-Unity-Examples-2017.4.3.0.unitypackage

Unityプロジェクト作成

HoloToolkitをインポートし、MixedRealityCameraParent、InputManager、DefaultCursorをHierarchyにD&Dします。
MixedRealityCameraParent->MixedRealityCameraの設定をします。

ボタンの作成

Hierarchy->UI->Buttonを作成します。CanvasのRender ModeをWorld Spaceにし、Rect Transformおよび、ButtonのRect Transformを調整し、カメラの前に来るようにします。
Button->Image(script)->Source ImageをHoloToolkitのサンプルにあるKeyboardKeyGlyphs_Shift_Symbolsにします。
focusedbutton01.PNG

Buttonの子オブジェクトにImageを作成します。Source ImageはKeyboardKeyGlyphs_Shift_Symbolsの円のみを残したものKeyboardKeyGlyphs_Circleを作りました。
この円の部分がゲージになります。Image TypeをFilled、Fill OrientationをTopにすれば、時計回りにゲージがたまります。
focusedbutton02.PNG

FocusedButtonManager.cs

FocusedButtonManager.csを作成し、Buttonにアタッチします。注視したことを判定するためにBox Collidarをアタッチします。Collidarのサイズを調整してください。
ボタンに視線を合わせてからゲージがたまるまでの時間がwaitTimeになります。
Do Something Hereのところに、注視入力でやりたいことを記述してください。
今回はCubeのScaleを変えてみました。

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

public class FocusedButtonManager : MonoBehaviour, IFocusable
{

    public Image image;
    private float waitTime = 2.0f;
    private bool progress;
    public GameObject cube;

    // Use this for initialization
    void Start()
    {
        image.fillAmount = 0.0f;
        progress = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (progress == true)
        {
            image.fillAmount += 1.0f / waitTime * Time.deltaTime;
            if (image.fillAmount >= 1.0f)
            {
                /*--- Do Something Here ---*/
                cube.transform.localScale += new Vector3(0.1f, 0.1f, 0.1f);
                /*--- Do Something End ---*/
                image.fillAmount = 0.0f;
                progress = false;
            }
        }
        else
        {
            image.fillAmount = 0.0f;
        }
    }

    public void OnFocusEnter()
    {
        progress = true;
    }

    public void OnFocusExit()
    {
        progress = false;
    }
}

実行

ボタンに視線を合わせると、ゲージがたまり、Cubeが大きくなります。
ゲージがたまっている途中で視線を外すと、ゲージがリセットされます。

ソースコードはこちら

参考文献

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