微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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循环.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐