zoukankan      html  css  js  c++  java
  • 【转】Unity3d中制作Loading场景进度条所遇到的问题 LoadLevelAsync,AsyncOperation

    背景

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

    尝试——遇到问题

    第一步当加载完Loading场景后,调用如下的LoadGame函数开始加载游戏场景,使用异步加载的方式加载场景1(Loading场景为0,主场景为1),通过Unity提供的Coroutine机制,我们可以方便的在每一帧结束后调用SetLoadingPercentage函数来更新界面中显示的进度条的数值。

    public void LoadGame() {
        StartCoroutine(StartLoading_1(1));
    }
    
    private IEnumerator StartLoading_1(int scene) {
        AsyncOperation op = Application.LoadLevelAsync(scene);
        while(!op.isDone) {            
            SetLoadingPercentage(op.progress * 100);
            yield return new WaitForEndOfFrame();
        }        
    }
    

    最后进度条的效果显示如下:

    进度条并没有连续的显示加载的进度,而是停顿一下切换一个数字,再停顿一下切换一个数子,最后在没有显示100%就情况下就切换到主场景了。究其原因在于Application.LoadLevelAsync并不是真正的后台加载,它在每一帧加载一些游戏资源,并给出一个progress值,所以在加载的时候还是会造成游戏卡顿,AsyncOperation.progress的值也不够精确。当主场景加载完毕后Unity就自动切换场景,所以上述代码中的while循环体内的代码是不会被调用的,导致进度条不会显示100%。

    修补——100%完成

    为了让进度条能显示100%,取巧一点的办法是将AsyncOperation.progress的值乘上2,这样当加载到50%的时候界面上就显示100%了。缺点是当界面上显示100%的时候,用户还要等待一段时间才会进入游戏。其实Unity提供了手动切换场景的方法,把AsyncOperation.allowSceneActivation设为false就可以禁止Unity加载完毕后自动切换场景,修改后的StartLoading_2代码如下:

    // this function is not work
    private IEnumerator StartLoading_2(int scene) {
        AsyncOperation op = Application.LoadLevelAsync(scene);
        op.allowSceneActivation = false;
        while(!op.isDone) {
            SetLoadingPercentage(op.progress * 100);
            yield return new WaitForEndOfFrame();
        }
        op.allowSceneActivation = true;   
    }
    

    我们首先将AsyncOperation.allowSceneActivation设为false,当加载完成后再设为true。代码看上去没有错,但是执行的结果是进度条最后会一直停留在90%上,场景不会切换。通过打印log发现AsyncOperation.isDone一直为falseAsyncOperation.progress的值增加到0.9后就保持不变了,也就是说场景永远不会被加载完毕。

    在这个帖子中找到了答案,原来把allowSceneActivation设置为false后,Unity就只会加载场景到90%,剩下的10%要等到allowSceneActivation设置为true后才加载,这不得不说是一个坑。所以代码改为如下。当AsyncOperation.progress到达0.9后,就直接把进度条的数值更新为100%,然后设置AsyncOperation.allowSceneActivationture,让Unity继续加载未完成的场景。

    private IEnumerator StartLoading_3(int scene) {
        AsyncOperation op = Application.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;   
    }
    

    最后的效果如下:

    打磨——增加动画

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

    private IEnumerator StartLoading_4(int scene) {
        int displayProgress = 0;
        int toProgress = 0;
        AsyncOperation op = Application.LoadLevelAsync(scene);
        op.allowSceneActivation = 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;
    }
    

    displayProgress用来记录要显示在进度条上的数值,最后进度条的动画如下:

    对比第一种的进度条

    总结

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

    https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html

    https://docs.unity3d.com/ScriptReference/AsyncOperation.html

    AsyncOperation

     

    Description

    Asynchronous operation coroutine.

    You can yield until asynchronous operation continues, or manually check whether it's done (isDone) or progress (progress).

    See Also: SceneManager.LoadSceneAsync, AssetBundle.LoadAsync.

    Variables

    allowSceneActivation Allow scenes to be activated as soon as it is ready.
    isDone Has the operation finished? (Read Only)
    priority Priority lets you tweak in which order async operation calls will be performed.
    progress What's the operation's progress. (Read Only)

     

  • 相关阅读:
    精华网站链接
    面向对象的CSS注意事项
    java 汉字按照指定字符集转换为二进制编码 并把二进制编码转换为对应的汉字 方法
    91环境
    windows下git代码提交情况统计
    git分支管理的使用案例及深入分析
    我在软件园二期的小花园捡起了一片树叶
    sslvpn连接:
    jetbrains register ways
    angular directive实现触底加载效果一则
  • 原文地址:https://www.cnblogs.com/mimime/p/6528104.html
Copyright © 2011-2022 走看看