本文主要是介绍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 | 新的照明场景淡入时,上一个光照场景淡出 | 时间转换 |
如果希望平滑视觉对象的转换,这些设置必须保持一致
设置 | FadeToBlack | CrossFade |
---|---|---|
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场景管理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!