コンテンツの読み込みの監視 — MRTK2

シーン操作の進行状況

コンテンツが読み込まれているかアンロードされているとき、SceneOperationInProgress プロパティでは true が返されます。 SceneOperationProgress プロパティを使用すると、この操作の進行状況を監視できます。

SceneOperationProgress 値は、現在のすべての非同期シーン操作の平均です。 コンテンツの読み込みの開始時は、SceneOperationProgress はゼロになります。 完全に完了すると、SceneOperationProgress は 1 に設定され、次の操作が行われるまで 1 のままになります。 これらのプロパティに影響するのはコンテンツ シーン操作のみであることに注意してください。

これらのプロパティには、操作に複数のステップが含まれている場合でも、開始から終了までの "操作全体" の状態が反映されます。

IMixedRealitySceneSystem sceneSystem = MixedRealityToolkit.Instance.GetService<IMixedRealitySceneSystem>();

// First do an additive scene load
// SceneOperationInProgress will be true for the duration of this operation
// SceneOperationProgress will show 0-1 as it completes
await sceneSystem.LoadContent("ContentScene1");

// Now do a single scene load
// This will result in two actions back-to-back
// First "ContentScene1" will be unloaded
// Then "ContentScene2" will be loaded
// SceneOperationInProgress will be true for the duration of this operation
// SceneOperationProgress will show 0-1 as it completes
sceneSystem.LoadContent("ContentScene2", LoadSceneMode.Single)

進行状況の例

SceneOperationInProgress は、コンテンツの読み込み中にアクティビティを中断する必要がある場合に便利です。

public class FooManager : MonoBehaviour
{
    private void Update()
    {
        IMixedRealitySceneSystem sceneSystem = MixedRealityToolkit.Instance.GetService<IMixedRealitySceneSystem>();

        // Don't update foos while a scene operation is in progress
        if (sceneSystem.SceneOperationInProgress)
        {
            return;
        }

        // Update foos
        ...
    }
    ...
}

SceneOperationProgress を使用すると、進行状況ダイアログを表示できます。

public class ProgressDialog : MonoBehaviour
{
    private void Update()
    {
        IMixedRealitySceneSystem sceneSystem = MixedRealityToolkit.Instance.GetService<IMixedRealitySceneSystem>();

        if (sceneSystem.SceneOperationInProgress)
        {
            DisplayProgressIndicator(sceneSystem.SceneOperationProgress);
        }
        else
        {
            HideProgressIndicator();
        }
    }
    ...
}

アクションによる監視

シーン システムには、シーンの読み込みまたはアンロード時に通知するためのアクションがいくつか用意されています。 各アクションには、影響を受けるシーンの名前が反映されています。

読み込みまたはアンロード操作に複数のシーンが関係している場合は、影響を受けるシーンごとに関連するアクションが 1 回呼び出されます。 また、読み込み操作またはアンロード操作が "完全に完了" したときに、それらがすべて同時に呼び出されます。このため、完了後に OnUnloaded アクションを使用して破棄されたコンテンツを検出するのではなく、OnWillUnload アクションを使用して破棄 "される" コンテンツを検出することをお勧めします。

逆に、OnLoaded アクションはすべてのシーンがアクティブ化されて完全に読み込まれたときにのみ呼び出されるため、OnLoaded アクションを使用して新しいコンテンツを検出して使用することは安全であることが保証されます。

アクション 呼び出されるタイミング コンテンツのシーン 照明シーン マネージャーのシーン
OnWillLoadContent コンテンツ シーンの読み込みの直前
OnContentLoaded 読み込み操作のすべてのコンテンツ シーンが完全に読み込まれてアクティブ化された後
OnWillUnloadContent コンテンツ シーンのアンロード操作の直前
OnContentUnloaded アンロード操作のすべてのコンテンツ シーンが完全にアンロードされた後
OnWillLoadLighting 照明シーンの読み込みの直前
OnLightingLoaded 照明シーンが完全に読み込まれてアクティブ化された後
OnWillUnloadLighting 照明シーンのアンロードの直前
OnLightingUnloaded 照明シーンが完全にアンロードされた後
OnWillLoadScene シーンの読み込みの直前
OnSceneLoaded 操作のすべてのシーンが完全に読み込まれてアクティブ化された後
OnWillUnloadScene シーンのアンロードの直前
OnSceneUnloaded シーンが完全にアンロードされた後

アクションの例

アクションと Update ではないコルーチンを使用した、もう 1 つの進行状況に関するダイアログの例:

public class ProgressDialog : MonoBehaviour
{
    private bool displayingProgress = false;

    private void Start()
    {
        IMixedRealitySceneSystem sceneSystem = MixedRealityToolkit.Instance.GetService<IMixedRealitySceneSystem>();
        sceneSystem.OnWillLoadContent += HandleSceneOperation;
        sceneSystem.OnWillUnloadContent += HandleSceneOperation;
    }

    private void HandleSceneOperation (string sceneName)
    {
        // This may be invoked multiple times per frame - once per scene being loaded or unloaded.
        // So filter the events appropriately.
        if (displayingProgress)
        {
            return;
        }

        displayingProgress = true;
        StartCoroutine(DisplayProgress());
    }

    private IEnumerator DisplayProgress()
    {
        IMixedRealitySceneSystem sceneSystem = MixedRealityToolkit.Instance.GetService<IMixedRealitySceneSystem>();

        while (sceneSystem.SceneOperationInProgress)
        {
            DisplayProgressIndicator(sceneSystem.SceneOperationProgress);
            yield return null;
        }

        HideProgressIndicator();
        displayingProgress = false;
    }

    ...
}

シーンのアクティブ化の制御

既定では、コンテンツ シーンは読み込み時にアクティブ化するように設定されます。 シーンのアクティブ化を手動で制御する場合は、任意のコンテンツ読み込みメソッドに SceneActivationToken を渡すことができます。 複数のコンテンツ シーンが 1 回の操作で読み込まれる場合、このアクティブ化トークンはすべてのシーンに適用されます。

IMixedRealitySceneSystem sceneSystem = MixedRealityToolkit.Instance.GetService<IMixedRealitySceneSystem>();

SceneActivationToken activationToken = new SceneActivationToken();

// Load the content and pass the activation token
sceneSystem.LoadContent(new string[] { "ContentScene1", "ContentScene2", "ContentScene3" }, LoadSceneMode.Additive, activationToken);

// Wait until all users have joined the experience
while (!AllUsersHaveJoinedExperience())
{
    await Task.Yield();
}

// Let scene system know we're ready to activate all scenes
activationToken.AllowSceneActivation = true;

// Wait for all scenes to be fully loaded and activated
while (sceneSystem.SceneOperationInProgress)
{
    await Task.Yield();
}

// Proceed with experience

読み込まれるコンテンツを確認する

ContentSceneNames プロパティでは、ビルド インデックスの順に使用可能なコンテンツ シーンの配列が提供されます。 IsContentLoaded(string contentName) を使用すると、これらのシーンが読み込まれるかどうかを確認できます。

IMixedRealitySceneSystem sceneSystem = MixedRealityToolkit.Instance.GetService<IMixedRealitySceneSystem>();

string[] contentSceneNames = sceneSystem.ContentSceneNames;
bool[] loadStatus = new bool[contentSceneNames.Length];

for (int i = 0; i < contentSceneNames.Length; i++>)
{
    loadStatus[i] = sceneSystem.IsContentLoaded(contentSceneNames[i]);
}