异步加载进度控制加载进度条的显示

16 篇文章 2 订阅

代码:

public void LoadGameMethod()
    {
        StartCoroutine(StartLoading_4(2));
    }
    private IEnumerator StartLoading_4(int scene)
    {
        int displayProgress = 0;
        int toProgress = 0;
        AsyncOperation op = SceneManager.LoadSceneAsync(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;
    }
    private void SetLoadingPercentage(float v)
    {
        processView.value = v / 100;
    }**
   

解释:

SceneManager.LoadSceneAsync(scene); 异步加载函数返回的是一个协程对象,定义AsyncOperation 对象用来接收,AsyncOperation 类中的allowSceneActivation的bool属性变量,为false的时候,该类中的鸡杂游戏的进度pocess属性值保持在0.9,不再变化,为true的时候会继续记载剩余的0.1,另外剩余的0.1加载速度是非常快的,几毫秒就会完成,所以先将allowSceneActivation值设为false,让现实的进度条从0开始记载到0.89,然后再直接从0.89加载完成,最后把allowSceneActivation设为true,这样玩家看到的效果就是当进度条完成后直接进入下一个场景,不会看到还未加载完成的下一个场景。
WaitForEndofFrame()函数等待camera和GUI渲染结束,在加载下一帧**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值