【问题标题】:How do I properly reload a Unity Scene that uses MRTK for the Hololens2?如何正确重新加载将 MRTK 用于 Hololens2 的 Unity 场景?
【发布时间】:2021-03-12 12:21:47
【问题描述】:

这是我第一个使用 Hololens 和 MRTK for Unity 的项目。我想制作一个按钮,在按下时重新启动玩家的体验。

我使用以下代码将一个按钮连接到一个新脚本:

SceneManager.LoadScene(SceneManager.GetActiveScene().name);

按下按钮时,场景似乎会重新加载,但所有 MRTK/Hololens 功能都会停止。手部和眼球追踪没有反应,我无法与全息图互动。

我知道我可以手动创建一个重置方法来移动场景中的所有对象并重置特定脚本,但我正在尝试通过按下按钮来完全卸载和重新加载尽可能多的 Unity 应用程序。

那么,如何在 Hololens2 上正确地重新加载 Unity 场景?我仍在对此进行研究,并将更新我在这里找到的内容。非常感谢这里的任何帮助,谢谢!

【问题讨论】:

    标签: c# unity3d virtual-reality hololens mrtk


    【解决方案1】:

    MRTK 实际上有一个场景系统,它巧妙地封装了加载/卸载场景以及照明等可选概念。

    在此处查看文档/指南:https://microsoft.github.io/MixedRealityToolkit-Unity/Documentation/SceneSystem/SceneSystemGettingStarted.html

    【讨论】:

    • 是的,似乎他们终于自己添加了这样一个附加场景加载器,因为一直存在如何处理多个场景的问题^^最终它基本上与this相同,但可能默认场景可以更好地了解着陆场景中的实际需求
    【解决方案2】:

    对于 MRTK 的这类问题,总是避免使用它。


    我宁愿建议:

    在构建索引0 有一个 MRTK 和着陆场景。这个你永远不会卸载或重新加载。在这里,您还可以放置负责(重新)加载内容的管理器脚本。

    然后通过additive scene loading 加载其他所有内容,然后仅卸载并重新加载这些额外的场景。

    这使 MRTK 保持不变,您仍然可以加载、卸载和重新加载您的应用内容。


    提到的经理类可能看起来有点像

    // Put this into the MRTK scene
    public class SceneLoader : MonoBehaviour
    {
        private void Awake ()
        {
            // Register callback for everytime a new scene is loaded
            SceneManager.sceneLoaded += OnSceneLoaded;
    
            // Initially load the second scene additive
            SceneManager.LoadScene(1, LoadSceneMode.Additive);
        }
    
        // Called when a new scene was loaded
        private void OnSceneLoaded(Scene scene, LoadSceneMode loadMode) 
        {
            if(loadMode == LoadSceneMode.Additive)
            {
                // Set the additive loaded scene as active
                // So new instantiated stuff goes here
                // And so we know which scene to unload later
                SceneManager.SetActiveScene(scene);
            }
        }
            
    
        public static void ReloadScene()
        {
            var currentScene = SceneManager.GetActiveScene();
            var index = currentScene.buildIndex;
    
            SceneManager.UnloadScene(currentScene);
    
            SceneManager.LoadScene(index, LoadSceneMode.Additive);
        }
    
        public static void LoadScene(string name)
        {
            var currentScene = SceneManager.GetActiveScene();
    
            SceneManager.UnloadScene(currentScene);
    
            SceneManager.LoadScene(name, LoadSceneMode.Additive);
        }
    
        public static void LoadScene(int index)
        {
            var currentScene = SceneManager.GetActiveScene();
    
            SceneManager.UnloadScene(currentScene);
    
            SceneManager.LoadScene(index, LoadSceneMode.Additive);
        }
    }
    

    然后重新加载当前场景

    SceneLoader.ReloadScene();
    

    或像往常一样通过

    加载不同的场景
    SceneLoader.LoadScene("SceneName");
    

    SceneLoader.LoadScene(buildIndex);
    

    当然,您也可以按照 API 示例实现异步版本。

    【讨论】:

    • 我不确定为什么这个问题被否决了。看来我必须按照这些思路做一些事情,根据我最初阅读的所有文档,这个答案并不是很明显。感谢您抽空 derHugo!
    猜你喜欢
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    相关资源
    最近更新 更多