MRTK场景管理

一、搭建环境

参考资料:
1.1 官方文档
1.2 【unity2020.3】 运行官方 HoloLens vuforia sample 保姆级教程
注意:在新工程配置!!配置好MRTK和Vuforia的工程再导入Vuforia Hololens 2 Sample会报错!!!
1.3 【unity2020.3】手把手教你——自定义图片的HoloLens2 vuforia识别
1.4 Unity开发——Hololens2+Vuforia实现图像识别详细教程
场景中必须包含且只能包含一个MixedRealityToolkit脚本的实例(Mixed Reality Toolkit-> Add to Scene and Configure),负责注册、更新和销毁服务。

二、场景管理

研究 VuforiaAdditiveSceneLoader.cs 以及 Vuforia Engine 如何 在多个场景中与MRTK 场景系统一起工作。

2.1添加其他场景内容:

IMixedRealitySceneSystem sceneSystem = MixedRealityToolkit.Instance.GetService<IMixedRealitySceneSystem>();
// 添加一个场景内的内容
await sceneSystem.LoadContent("MyContentScene");
// 同时添加多个场景内的内容
await sceneSystem.LoadContent(new string[] { "MyContentScene1", "MyContentScene2", "MyContentScene3" });

2.2单个场景加载内容

IMixedRealitySceneSystem sceneSystem = MixedRealityToolkit.Instance.GetService<IMixedRealitySceneSystem>();
// 原有场景的内容会被保留
await sceneSystem.LoadContent("ContentScene1");
await sceneSystem.LoadContent("ContentScene2");
await sceneSystem.LoadContent("ContentScene3");
// LoadSceneMode.Single 将首先卸载所有已加载的内容场景,然后再继续加载。
await sceneSystem.LoadContent("SingleContentScene", LoadSceneMode.Single);

2.3加载下一个/上一个场景

默认使用 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。

或者
//参数为 true,则内容将循环回第一个/最后一个生成索引。 这无需检查NextContentExists
IMixedRealitySceneSystem sceneSystem = MixedRealityToolkit.Instance.GetService<IMixedRealitySceneSystem>();
if (nextSceneRequested)
{
    await sceneSystem.LoadNextContent(true);
}
if (prevSceneRequested)
{
    await sceneSystem.LoadPrevContent(true);
}

2.4 按标记加载场景

IMixedRealitySceneSystem sceneSystem = MixedRealityToolkit.Instance.GetService<IMixedRealitySceneSystem>();
await LoadContentByTag("Stage1");

// Wait until stage 1 is complete
await UnloadContentByTag("Stage1");
await LoadContentByTag("Stage2);

2.5 编辑器加载场景

默认情况下,场景系统利用一对场景对象) (DefaultManagerScene 和 DefaultLighting 场景。 如果无法找到其中的任何一种情况,则会在场景系统配置检查器中出现一条消息。
在这里插入图片描述
可以咋在UPM内导入:
在这里插入图片描述
在这里插入图片描述

编辑器设置
Editor Manage Build Settings如果为 true,确保添加所有managers, lighting 、content scenes。 如果希望完全控制生成设置,请禁用此设置
Editor Enforce Scene Order如果为 true,确保manager scene在hierarchy的第一位
Editor Manage Loaded Scenes如果为 true,确保managers, lighting 、content scenes不被销毁
Editor Enforce Lighting Scene Types如果为 true,确保Lighting Scene只有PermittedLightingSceneComponentTypes定义的类型

在这里插入图片描述

2.6 Lighting scene 光照场景切换

在启动时加载配置文件中定义的 默认照明场景。 在调用SetLightingScene之前,保持加载状态。

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

transitionType 控制新光源场景的过渡到样式

IMixedRealitySceneSystem sceneSystem = MixedRealityToolkit.Instance.GetService<IMixedRealitySceneSystem>();
sceneSystem.SetLightingScene("MiddayLighting", LightingSceneTransitionType.CrossFade);
类型描述效果推荐
无转换-打咩-
FadeToBlack旧-黑-新空间转换
CrossFade新的照明场景淡入时,上一个光照场景淡出时间转换

如果希望平滑视觉对象的转换,这些设置必须保持一致

设置FadeToBlackCrossFade
Skybox不支持不支持
custom reflection不支持不支持
Sun Light Realtime Shadows支持不支持

三、场景加载/卸载操作

3.1 场景加载进度

加载或卸载内容时 SceneOperationInProgress 返回 true
内容加载开始时 SceneOperationProgress 为0,完成后为1
这些属性标志 从场景变化开始到 整个操作完成的过程(即包括多个步骤)的状态

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
        ...
    }
    ...
}

显示进度对话框

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

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

3.2 加载/卸载过程中内容检测

  • OnUnloaded:已销毁的内容
  • OnLoaded:完全加载所有场景时才调用
  • OnWillUnload:将 销毁的内容
操作调用时内容场景照明场景管理器场景
OnWillLoadContent在内容场景加载之前
OnContentLoaded已完全加载并激活加载操作中的所有内容场景后
OnWillUnloadContent在内容场景卸载操作之前
OnContentUnloaded卸载操作中的所有内容场景完全卸载后
OnWillLoadLighting就在照明场景负载之前
OnLightingLoaded已完全加载并激活光照场景后
OnWillUnloadLighting在光照场景卸载之前
OnLightingUnloaded完全卸载照明场景后
OnWillLoadScene就在场景加载之前
OnSceneLoaded完全加载和激活操作中的所有场景后
OnWillUnloadScene刚卸载场景之前
OnSceneUnloaded完全卸载场景后

状态监听 +=添加

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;
    }

    ...
}

3.3 控制场景激活

默认情况下,内容场景设置为在加载时激活。
如果要手动控制场景激活,则可以传递 SceneActivationToken 到任何内容加载方法。
如果一个操作正在加载多个内容场景,将应用于所有场景。

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

3.4 检查加载的内容

**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]);
}

四、场景类型

scene type
Content scene默认启用场景,可以加载或卸载配置文件的 Content Scenes 数组中包含的任何场景
Manager scene包含MixedRealityToolkit 实例(只允许一个)。启动时最先加载,并将程序生命周期保持加载。还可以托管不应销毁的其他对象,作为 DontDestroyOnLoad 的替代方法
lighting scene勾选Use Lighting Scene ,用来存储光照信息和照明对象,来确保光照场景保持一致。Lighting Scenes 数组内添加光照场景,只能加载一个,但可以实现切换。

照明设置缓存:
配置文件保存照明场景中的照明设置的 缓存副本 ,如果光照场景中发生变化则需要更新缓存,以确保光照在播放模式下显示为预期状态。
在这里插入图片描述

参考资料:
https://docs.microsoft.com/zh-cn/windows/mixed-reality/mrtk-unity/features/scene-system/scene-system-content-loading?view=mrtkunity-2021-05

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值