赞
踩
当使用异步加载场景时,progress的值永远都不会达到1,当到达0.9时直接跳转场景,使进度条看上去很不平滑,可以通过以下的写法完善异步加载的进度条
将此脚本挂载到进度条身上
- using UnityEngine.UI;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using System.Collections;
-
- public class LoadGame : MonoBehaviour
- {
- private Slider loadingSlider;//加载进度条
-
- private void Start()
- {
- loadingSlider = GetComponent<Slider>();
-
- StartCoroutine(LoadScene());
- }
-
- //加载场景
- private IEnumerator LoadScene()
- {
- int displayProgress = 0;
- int toProgress = 0;
- AsyncOperation op = SceneManager.LoadSceneAsync(2);
- 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)
- {
- loadingSlider.value = v / 100;
- }
- }
将此脚本挂载到进度条身上
- using UnityEngine.UI;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using System.Collections;
-
- public class LoadGame : MonoBehaviour
- {
- private Slider loadingSlider;//加载进度条
-
- private AsyncOperation async;//异步加载的返回值
-
- private int nowprocess = 0;//当前进度
-
- void Start()
- {
- loadingSlider = GetComponent<Slider>();
-
- StartCoroutine(LoadScene());
- }
-
- //加载场景
- IEnumerator LoadScene()
- {
- async = SceneManager.LoadSceneAsync(2);
- async.allowSceneActivation = false;//设置加载完成后不能自动跳转场景
- yield return async;
- }
-
- void Update()
- {
- if (async == null)
- {
- return;
- }
-
- int toProcess;//进度条需要到达的进度值
-
- if (async.progress < 0.9f)
- {
- toProcess = (int)(async.progress * 100);
- }
- else
- {
- toProcess = 100;
- }
-
- if (nowprocess < toProcess)
- {
- nowprocess++;
- }
-
- // 设置滑动条的value
- loadingSlider.value = nowprocess / 100f;
-
- //如果滑动条的值等于100,说明加载完毕
- if (nowprocess == 100)
- {
- async.allowSceneActivation = true;
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。