しっぽを追いかけて

ぐるぐるしながら考えています

Unity と猫の話題が中心   掲載内容は個人の私見であり、所属組織の見解ではありません

MRTK の Dialog を使ってみる

※ これは 2018/05/25 Unity 2017.4.1f1、Mixed Reality Toolkit 2017.4.0.0 Release Candidate 時点の情報です

最新版では動作が異なる可能性がありますのでご注意ください

Progress の次は Dialog のサンプルをお試し

github.com

必要環境は下記の通り

環境 必要条件
Unity 2017.1 以降が必須、2017.2 以降推奨(5.6 だと問題があるらしい)
Windows SDK 10.0.16299 以降
Visual Studio 2017 以降
Windows 10 Fall Creators Update 以降

お決まりの手順、MRTK のリリースページにある unitypackage 2つをそれぞれダウンロードして Unity 上にインポートします

UnityEditor を開いたら [Mixed Reality Toolkit] - [Configure] - [Apply Mixed Reality Project Settings] のメニューだけ実行してプロジェクト設定を調整

次に Assets/HoloToolkit-Examples/UX/Scenes/DialogsExample のサンプル実装のシーンを開いて実行!

OK ボタンのみ、2 ボタンの選択の2種類のダイアログが試せました

Dialog の表示部分のコードを見てみると・・・

        /// <summary>
        /// This function is called to set the settings for the dialog and then open it.
        /// </summary>
        /// <param name="buttons">Enum describing the number of buttons that will be created on the Dialog</param>
        /// <param name="title">This string will appear at the top of the Dialog</param>
        /// <param name="message">This string will appear in the body of the Dialog</param>
        /// <returns>IEnumerator used for Coroutine funtions in Unity</returns>
        protected IEnumerator LaunchDialog(DialogButtonType buttons, string title, string message)
        {
            isDialogLaunched = true;

            //Open Dialog by sending in prefab
            Dialog dialog = Dialog.Open(dialogPrefab.gameObject, buttons, title, message);

            if(dialog != null)
            {
                //listen for OnClosed Event
                dialog.OnClosed += OnClosed;
            }

            // Wait for dialog to close
            while (dialog.State < DialogState.InputReceived)
            {
                yield return null;
            }

            //only let one dialog be created at a time
            isDialogLaunched = false;

            yield break;
        }

        private void OnButtonClicked(GameObject obj)
        {
            if (isDialogLaunched == false)
            {
                if (numButtons == 1)
                {
                    // Launch Dialog with single button
                    StartCoroutine(LaunchDialog(DialogButtonType.OK, "Single Button Dialog", "Dialogs and flyouts are transient UI elements that appear when something happens that requires notification, approval, or additional information from the user."));
                }
                else if (numButtons == 2)
                {
                    // Launch Dialog with two buttons
                    StartCoroutine(LaunchDialog(DialogButtonType.Yes | DialogButtonType.No, "Two Buttons Dialog", "Dialogs and flyouts are transient UI elements that appear when something happens that requires notification, approval, or additional information from the user."));
                }
            }
        }

こんな感じで、Dialog.Open() メソッドを呼び出すだけでした

第一引数に Dialog の Prefab の GameObject、第二引数に表示するボタンの指定、第三引数 タイトル文字列、第四引数 メッセージ文字列ですね

第二引数に指定するボタンの指定は下記のようなフラグ列挙型になっています

    /// <summary>
    /// Enum describing the style (caption) of button on a Dialog.
    /// </summary>
    public enum DialogButtonType
    {
        None = 0,
        Close = 1,
        Confirm = 2,
        Cancel = 4,
        Accept = 8,
        Yes = 16,
        No = 32,
        OK = 64,
    }

最大2つまで指定でき、3つ以上指定するとボタン非表示になるようです

Dialog のボタンを押した結果は、OnClosed イベントハンドラに渡される DialogResult.Result プロパティに先ほどの DialogButtonType として確認できます

        /// <summary>
        /// Event Handler that fires when Dialog is closed- when a button on the Dialog is clicked.
        /// </summary>
        /// <param name="result">Returns a description of the result, which button was clicked</param>
        protected void OnClosed(DialogResult result)
        {
            // Get the result text from the Dialog
            resultTextMesh.text = result.Result.ToString();
        }

かんたんですね