场景系统内容加载 - 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);

下一个/上一个场景加载

可以按照生成索引的顺序单一加载内容。 对于引导用户逐一完成一组演示场景的展示应用,这非常有用。

Current scenes in build in player settings

请注意,下一个/上一个内容加载默认使用 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);
}

按标记加载

Loading content scenes by tag

有时需要按组加载内容场景。 例如,一个体验阶段可能由多个场景组成,所有场景都必须同时加载才能发挥作用。 为方便此操作,可以标记场景,然后通过该标记加载或卸载这些场景。

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 结构
VegetationTools 植被
Mountain 地形
Cabin 结构
植被

最终内容

场景名称 场景标记 由脚本加载
DebugTerrainPhysics DoNotInclude
StructureTesting DoNotInclude
VegetationTools DoNotInclude
Mountain 地形
Cabin 结构
植被

编辑器行为

可以使用场景系统的服务检查器在编辑器和播放模式中执行所有这些操作。在编辑模式下场景加载将是即时的,而在播放模式下,你可以观察加载进度并使用激活令牌

Scene system in the inspector with content loading highlighted