using CG.Framework; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; /******************************************************************************* *Create By CG *Function 异步场景加载控制(无界面显示) *******************************************************************************/ namespace CG.UTility { public class LoadScenesAsyncNoPanel : MonoSingleton { private bool _isLoadScene;//是否加载场景 private AsyncOperation _async; private int _toProgress = 0;//读取场景进度 private int _nowProgress = 0;//当前场景返回进度 private System.Action _loadedCall = null; private void Update() { if (_isLoadScene) { if (_async == null) return; if (_async.progress < 0.9f) { _toProgress = (int)_async.progress * 100; } else { _toProgress = 100; } if (_nowProgress < _toProgress) { _nowProgress++; } if (_nowProgress == 100) { if (_async.isDone) { _loadedCall?.Invoke(); _isLoadScene = false; StopCoroutine("LoadScene"); transform.Find("EventSystem").gameObject.SetActive(true); Framework.UI_Manage.Instance.ClosePanel("LoadingSceenPanel"); } _async.allowSceneActivation = true; } } } /// /// 加载场景主方法 /// /// public void LoadSceneAsync(string sceneName, System.Action LoadedCall) { _loadedCall = LoadedCall; _toProgress = 0; _nowProgress = 0; _isLoadScene = true; StartCoroutine("LoadScene", sceneName); Framework.UI_Manage.Instance.ShowPanel("LoadingSceenPanel", System.Type.GetType("CG.Framework.LoadingSceenPanel"), Framework.UIGroup.Tip); transform.Find("EventSystem").gameObject.SetActive(false); } private IEnumerator LoadScene(string sceneName) { _async = SceneManager.LoadSceneAsync(sceneName); _async.allowSceneActivation = false; yield return new WaitForEndOfFrame();//加上这句可以先让加载画面显示 yield return _async; } } }