c# – 在后台加载新场景
作者:互联网
我正在创建一个针对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循环.
标签:c,unity3d,unity5 来源: https://codeday.me/bug/20190611/1218757.html