zoukankan      html  css  js  c++  java
  • Unity3D中制作Loading场景进度条

    背景

    通常游戏的主场景包含的资源较多,这会导致加载场景的时间较长。为了避免这个问题,可以首先加载Loading场景,然后再通过Loading场景来加载主场景。由于Loading场景包含的资源较少,所以加载速度快。在加载主场景时一般会在Loading界面中显示一个进度条来告知玩家当前加载的进度。在Unity中可以通过调用SceneManager.LoadLevelAsync来异步加载游戏场景,通过查询AsyncOperation.progress来得到场景加载的进度。

    而SceneManager.LoadLevelAsync并不是真正的后台加载,它在每一帧加载一些游戏资源,并给出一个progress值,所以加载的时候会造成游戏卡顿,AsyncOperation.progress的值也不够精确。当主场景加载完毕后Unity就自动切换场景,这可能会导致进度条不会显示100%。

    进度条显示100%

    Unity提供了手动切换场景的方法,把AsyncOperation.allowSceneActivation设置为false,就可以禁止Unity加载完毕后自动切换场景。但是这种方法的执行结果是进度条最后会一直停留在90&上,场景就不会切换,此时打印AsyncOperation.isDone一直是false,剩下的10%要等到AsyncOperation.allowSceneActivation设置为true后才加载完毕。

    所以当加载到90%后需要手动设置数值更新为100%,让Unity继续加载未完成的场景。

     private IEnumerator StartLoading_1 (int scene){
         AsyncOperation op = SceneManager.LoadLevelAsync(Scene);
         op.allowSceneActivation = false;
         while(op.progress <= 0.9f){
             SetLoadingPercentage(op.progress * 100);
             yield return new WaitForEndOfFrame();
         }
         SetLoadingPercentage(100);
         yield return new WaitForEndOfFrame();
         op.allowSceneActivation = true;
     
     }
     
     public void LoadGame(){
         StartCoroutine(StartLoading_1(1));
     }

    平滑显示进度条

    上述的进度条虽然解决了100%显示的问题,但由于进度条的数值更新不是连续的,所以看上去不够自然和美观。为了看上去像是在连续加载,可以每一次更新进度条的时候插入过度数值。比如:获得AsyncOperation.progress的值后,不立即更新进度条的数值,而是每一帧在原有的数值上加1,这样就会产生数字不停滚动的动画效果了。

    private IEnumerator StartLoading_2(int scene){
        int displayProgress = 0;
        int toProgress = 0;
        AsyncOperation op = SceneManager.LoadLevelAsync(scene);
        op.allowSecneActivation = false;
        while(op.progress < 0.9f){
            toProgress = (int)op.progress * 100;
            while(displayProgress < toProgress){
                ++displayProgress;
                SetLoadingPercentage(displayProgress);
                yield return new WaitForEndOfFrame();
            }       
        }
        toProgress = 100;
        while(displayProgress < toProgress){
                ++displayProgress;
                SetLoadingPercentage(displayProgress);
                yield return new WaitForEndOfFrame();
            }   
         op.allowSceneActivation = true;
    }

    总结:

    如果在加载游戏场景之前还需要解析数据表格,生成对象池,进行网络连接等操作,那么可以给这些操作赋予一个权值,利用这些权值就可以计算加载的进度了。如果你的场景加载速度非常快,那么可以使用一个假的进度条,让玩家看几秒钟loading动画,然后再加载场景。

  • 相关阅读:
    sp2010 升级sp2013 用户无法打开网站
    powerviot install in sharepoint 2013
    can not connect cube in performancce dashboard
    westrac server security configure user info
    添加报表服务在多服务器场
    sharepoint 2013 office web app 2013 文档在线浏览 IE11 浏览器不兼容解决方法
    delete job definition
    目前付款申请单内网打开慢的问题
    item style edit in sharepoint 2013
    Could not load file or assembly '$SharePoint.Project.AssemblyFullName$'
  • 原文地址:https://www.cnblogs.com/yeting-home/p/6248461.html
Copyright © 2011-2022 走看看