Lua实现 加载场景Loading进度

废话不多说 直接上代码!

local async = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(sceneName)
ShowLoadingAsync(async)
---异步显示loading进度
local function ShowLoadingAsync(async)
    local toProgress = 0
    local displayProgress = 0
    async.allowSceneActivation = false
    while (async.progress < 0.89) do
        toProgress = async.progress * 100
        while (displayProgress < toProgress) do
            displayProgress = displayProgress + 1
            this.LoadingProgress = displayProgress / 100.0
            LogicEventDispatcher.SendWithArgs(LogicEventType.LoadSceneProgress,this.LoadingProgress)
            coroutine.step()
        end
        coroutine.step()
    end

    toProgress = 100
    while (displayProgress < toProgress) do
        displayProgress = displayProgress + 1
        this.LoadingProgress = displayProgress / 100.0
        LogicEventDispatcher.SendWithArgs(LogicEventType.LoadSceneProgress,this.LoadingProgress)
        coroutine.step()
    end
    async.allowSceneActivation = true
    this.LoadingProgress = 1.0
end

把async.allowSceneActivation设置为false后,Unity就只会加载场景到0.9,那0.1要等到async.allowSceneActivation设置为true后才加载。所以当async.progress到达0.9后,我们手动设置进度条的数值1,然后设置async.allowSceneActivation为ture,让Unity继续加载那10%的场景,显示上做了一个平滑处理。lua代码为纯手打,第一次写博客,有不对的地方欢迎指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值