废话不多说 直接上代码!
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代码为纯手打,第一次写博客,有不对的地方欢迎指正。