我正在创建一个针对 Samsung Gear VR 的 Unity 应用程序。我目前有两个场景:
- 最初的场景
- 第二个场景,数据量大(加载场景需要太多时间)。
从第一个场景开始,我想在后台加载第二个场景,并在加载后切换到它。当新场景在后台加载时,用户应该保持移动头部的能力来查看 VR 环境的任何部分。
我在用着SceneManager.LoadSceneAsync
但它不起作用:
// ...
StartCoroutiune(loadScene());
// ...
IEnumerator loadScene(){
AsyncOperation async = SceneManager.LoadAsyncScene("Scene", LoadSceneMode.Single);
async.allowSceneActivation = false;
while(async.progress < 0.9f){
progressText.text = async.progress+"";
}
while(!async.isDone){
yield return null;
}
async.allowSceneActivation = true;
}
使用该代码,场景永远不会改变。
我已经尝试过这个典型的SceneManager.LoadScene("name")
在这种情况下,场景会在 30 秒后正确更改。
这应该有效
while(async.progress < 0.9f){
progressText.text = async.progress.ToString();
yield return null;
}
其次,我见过这样的案例isDone
永远不会设置为 true,除非场景已激活。删除这些行:
while(!async.isDone){
yield return null;
}
最重要的是,您将代码锁定在第一个 while 循环中。添加产量,以便应用程序可以继续加载您的代码。
所以你的整个代码如下所示:
IEnumerator loadScene(){
AsyncOperation async = SceneManager.LoadAsyncScene("Scene", LoadSceneMode.Single);
async.allowSceneActivation = false;
while(async.progress <= 0.89f){
progressText.text = async.progress.ToString();
yield return null;
}
async.allowSceneActivation = true;
}
不过,问题的最大罪魁祸首是第一个 while 循环中的锁定。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)