在后台加载新场景
Loading new scene in background
我正在创建一个针对 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 循环中。添加 yield 以便应用程序可以继续加载您的代码。
因此您的整个代码如下所示:
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 循环中的锁定。
我正在创建一个针对 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 循环中。添加 yield 以便应用程序可以继续加载您的代码。
因此您的整个代码如下所示:
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 循环中的锁定。