場景系統內容載入 — MRTK2

所有內容載入作業都是非同步,而且預設會加總所有內容載入。 管理員和光源場景永遠不會受到內容載入作業的影響。 如需監視載入進度和場景啟用的相關資訊,請參閱 監視內容載入

載入內容

若要載入內容場景,請使用 LoadContent 方法:

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

// Additively load a single content scene
await sceneSystem.LoadContent("MyContentScene");

// Additively load a set of content scenes
await sceneSystem.LoadContent(new string[] { "MyContentScene1", "MyContentScene2", "MyContentScene3" });

單一場景載入

單一場景載入的對等專案可以透過選擇性 mode 引數來達成。 LoadSceneMode.Single 會先卸載所有載入的內容場景,再繼續進行載入。

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

// ContentScene1, ContentScene2 and ContentScene3 will be loaded additively
await sceneSystem.LoadContent("ContentScene1");
await sceneSystem.LoadContent("ContentScene2");
await sceneSystem.LoadContent("ContentScene3");

// ContentScene1, ContentScene2 and ContentScene3 will be unloaded
// SingleContentScene will be loaded additively
await sceneSystem.LoadContent("SingleContentScene", LoadSceneMode.Single);

下一個/上一個場景載入

您可以依組建索引的順序來載入內容。 這適用于展示可讓使用者逐一流覽一組示範場景的應用程式。

播放機設定中組建中的目前場景

請注意,下一個/上一個內容載入預設會使用 LoadSceneMode.Single,以確保已卸載先前的內容。

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

if (nextSceneRequested && sceneSystem.NextContentExists)
{
    await sceneSystem.LoadNextContent();
}

if (prevSceneRequested && sceneSystem.PrevContentExists)
{
    await sceneSystem.LoadPrevContent();
}

PrevContentExists 如果至少有一個內容場景的組建索引低於目前載入的最低組建索引,則會傳回 true。 NextContentExists 如果至少有一個內容場景的組建索引高於目前載入的最高組建索引,則會傳回 true。

如果自 wrap 變數為 true,內容會回傳至第一個/最後一個組建索引。 這樣就不需要檢查下一個/上一個內容:

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

if (nextSceneRequested)
{
    await sceneSystem.LoadNextContent(true);
}

if (prevSceneRequested)
{
    await sceneSystem.LoadPrevContent(true);
}

依標記載入

依標記載入內容場景

有時最好在群組中載入內容場景。 例如,體驗的階段可能會由多個場景組成,這些場景都必須同時載入才能運作。 為了方便進行,您可以標記場景,然後使用該標籤載入或卸載它們。

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

await LoadContentByTag("Stage1");

// Wait until stage 1 is complete

await UnloadContentByTag("Stage1");
await LoadContentByTag("Stage2);

如果作者想要從體驗中納入/移除元素,而不需要修改腳本,則依標籤載入也很有用。 例如,使用下列兩組標記來執行此腳本會產生不同的結果:

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

await LoadContentByTag("Terrain");
await LoadContentByTag("Structures");
await LoadContentByTag("Vegetation");

測試內容

場景名稱 場景標籤 腳本載入
DebugTerrainPhysics 地形
StructureTesting 結構
一元 植被
Mountain 地形
船艙 結構
樹木 植被

最終內容

場景名稱 場景標籤 腳本載入
DebugTerrainPhysics DoNotInclude
StructureTesting DoNotInclude
一元 DoNotInclude
Mountain 地形
船艙 結構
樹木 植被

編輯器行為

您可以使用場景系統 的服務偵測器 ,在編輯器和播放模式中執行所有這些作業。在編輯模式場景載入將會立即完成,而在播放模式中,您可以觀察載入進度並使用 啟用權杖。

醒目提示內容載入的偵測器中的場景系統