Unity 异步加载进度条
1 public class View_LoadingScene : MonoBehaviour 2 { 3 //场景加载进度条对象 4 public GameObject loadingProgressBar; 5 //进度条组件 6 private Slider _loadingProgressBarComp; 7 //当前进度值 8 private float _curProgressValue = 0f; 9 10 private AsyncOperation _asyncOp; 11 private void Awake() 12 { 13 if(loadingProgressBar != null) 14 { 15 //获取进度条组件 16 _loadingProgressBarComp = loadingProgressBar.GetComponent<Slider>(); 17 } 18 } 19 private void Start() 20 { 21 22 StartCoroutine("LoadingLevelAsync"); 23 } 24 private void Update() 25 { 26 //模拟加载进度 27 if(_loadingProgressBarComp.value <= 1f) 28 { 29 SimulateLoad(); 30 } 31 _loadingProgressBarComp.value = _curProgressValue; 32 33 } 34 35 private void SimulateLoad() 36 { 37 _curProgressValue += 0.01f; 38 } 39 40 //异步加载核心函数 41 IEnumerator LoadingLevelAsync() 42 { 43 _asyncOp = SceneManager.LoadSceneAsync("2_LogonScene"); 44 _curProgressValue = _asyncOp.progress; 45 yield return _asyncOp; 46 } 47 }