夜風のMixedReality

xRと出会って変わった人生と出会った技術を書き残すためのGeekなHoloRangerの居場所

MRDL Surfacesを学ぶ Startup Scene シーンロード

本日はMRDLのSurfacesを勉強していきます。

前回は[HandMenu]を見ていきました。

今回はHandMenu内のボタンを見ていきます。

〇Lightning

f:id:Holomoto-Sumire:20210323090124j:plain

Surfacesの[HandMenu]ボタンはMRTKで提供されている[PresableButton]がベースとなっています。

redhologerbera.hatenablog.com

inspectorウィンドウからはMRTKで提供されているコンポーネントに加えSurfaces独自の[ButtonLoadContentScene]と呼ばれるコンポーネントがアタッチされています。

f:id:Holomoto-Sumire:20210323093333j:plain

[ButtonLoadContentScene]コンポーネントの冒頭には[#oragma warning disable 0649]というコードがあります。

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#pragma warning disable 0649 // For serialized fields
using Microsoft.MixedReality.Toolkit;
using Microsoft.MixedReality.Toolkit.Extensions.SceneTransitions;
using MRDL;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ButtonLoadContentScene : MonoBehaviour
{
...
}

気になったので調べてみるとこれは[Serialize Field]の属性を使用する際にCS0649のエラーが発生することがあり、これはUnityのバージョンや.Netのバージョンによって発生するようですが、このエラーをdisable(無効化)する処理です。

qiita.com

〇ButtonLoadContentScene

このコンポーネントはMRTKで提供されるシーン移行サービスを用いてシーンをロードします。

MRTKのドキュメントは次のようになります。

microsoft.github.io

MRが楽しい」のホロモンさんの記事にこれを読み解いたものがあり、非常に参考になります。

bluebirdofoz.hatenablog.com

  private void Update()
    {
        if (loadOnKeyPress && Input.GetKeyDown(keyCode))
            LoadContent();
    }

    public void LoadContent()
    {

        if (CoreServices.SceneSystem.SceneOperationInProgress)
            return;

        ISceneTransitionService transitions = MixedRealityToolkit.Instance.GetService<ISceneTransitionService>();
        transitions.DoSceneTransition(
            () => CoreServices.SceneSystem.LoadContent(contentName, loadSceneMode),
            () => SurfacePlacement.Instance.PlaceNewSurface());
    }

〇Update関数

  private void Update()
    {
        if (loadOnKeyPress && Input.GetKeyDown(keyCode))
            LoadContent();
    }

Update関数はデバッグ用に使用されています。 インスペクターウィンドウで設定した[keyCode]のボタンが押された際にSceneLodaが読み込まれシーン遷移します。もしくはインスペクターウィンドウから[loadOnKeyPress]のチェックボックスをオンにすることで子のデバッグが使用可能になります。

f:id:Holomoto-Sumire:20210324082642j:plain

〇LoadContent

        if (CoreServices.SceneSystem.SceneOperationInProgress)
            return;

シーンが現在読み込み中の場合は[CoreServices.SceneSystem.SceneOperationInProgress]がTrueが返されます。

これによってscene読み込み中に重複して処理が行われないようになっています。

  ISceneTransitionService transitions = MixedRealityToolkit.Instance.GetService<ISceneTransitionService>();
        transitions.DoSceneTransition(
            () => CoreServices.SceneSystem.LoadContent(contentName, loadSceneMode),
            () => SurfacePlacement.Instance.PlaceNewSurface());

MRTKのシーン遷移サービスでシーンを読み込みます。

CoreServices.SceneSystem.LoadContent(contentName, loadSceneMode)

microsoft.github.io

microsoft.github.io

[CoreServices.SceneSystem.LoadContent()]では第一引数として読み込むシーン名、第二引数としてモードが指定されています。

このモードはどのようにシーンをロードするのか(現在のシーンにどのように加算するのか?)を指定しており、[Additive]モードと[Single]の二つがあります。

[Additive]モードの場合シーンを複数読み込むことができます。

f:id:Holomoto-Sumire:20210324083938j:plain
Additiveモードに変更したため複数のシーンが加算されている

デフォルトで[Single]が指定されており、シーンが加算されずに上書きされるようになっています。

SurfacesのHandMenuではボタンを押すことで[LoadContent]が読み込まれ、シーンが読み込まれます。

以上でSurfacesアプリのシーンのロードの仕組みに関して学びました。