MRTK场景管理

2024-02-07 21:32
文章标签 管理 场景 mrtk

本文主要是介绍MRTK场景管理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 一、搭建环境
  • 二、场景管理
    • 2.1添加其他场景内容:
    • 2.2单个场景加载内容
    • 2.3加载下一个/上一个场景
    • 2.4 按标记加载场景
    • 2.5 编辑器加载场景
    • 2.6 Lighting scene 光照场景切换
    • 三、场景加载/卸载操作
      • 3.1 场景加载进度
      • 3.2 加载/卸载过程中内容检测
    • 3.3 控制场景激活
    • 3.4 检查加载的内容
  • 四、场景类型

一、搭建环境

参考资料:
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

这篇关于MRTK场景管理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/688992

相关文章

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Knife4j+Axios+Redis前后端分离架构下的 API 管理与会话方案(最新推荐)

《Knife4j+Axios+Redis前后端分离架构下的API管理与会话方案(最新推荐)》本文主要介绍了Swagger与Knife4j的配置要点、前后端对接方法以及分布式Session实现原理,... 目录一、Swagger 与 Knife4j 的深度理解及配置要点Knife4j 配置关键要点1.Spri

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

python中Hash使用场景分析

《python中Hash使用场景分析》Python的hash()函数用于获取对象哈希值,常用于字典和集合,不可变类型可哈希,可变类型不可,常见算法包括除法、乘法、平方取中和随机数哈希,各有优缺点,需根... 目录python中的 Hash除法哈希算法乘法哈希算法平方取中法随机数哈希算法小结在Python中,

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解