using Cysharp.Threading.Tasks; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using YooAsset; public class ScenesManager : MonoBehaviour { public static ScenesManager Instance { get; private set; } private AsyncOperation ao; public SceneHandle handle; //测试 private Image panel; private Image progress; private Text loading; //测试 private void Awake() { Instance = this; } private void Update() { if (/*SceneManager.GetActiveScene().name == "LoadingScene" && */progress == null && loading == null) { GameObject canvas = GameObject.Find("UIRoot").gameObject; panel = canvas.transform.Find("LoadingPanel").GetComponent(); progress = panel.transform.Find("LoadingBar/LoadingProgress").GetComponent(); loading = panel.transform.Find("ProgressTxt").GetComponent(); } //LoadingInProgress(); YooAssetLoadingProgress(); } /// /// 通过YooAsset加载场景 /// /// /// public async UniTask SceneLoadByYooAsset(string sceneName) { await YooAssetManager.Instance.LoadScene(sceneName); } /// /// YooAsset加载过程 /// void YooAssetLoadingProgress() { if (handle != null) { //Debug.Log(handle.Progress); if (progress != null) { progress.fillAmount = handle.Progress;// loading.text = (int)((handle.Progress) * 100) + "%";// } if (handle.Progress >= 0.9f) { if (progress != null) { progress.fillAmount = 1;// loading.text = 100 + "%";// } handle.UnSuspend(); UIManager.Instance.HideUI(WindowID.LoadingPanel); SceneManager.sceneLoaded += StepAdd; } } } /// /// 场景加载+SceneName /// /// public void SceneLoad(string sceneName) { StartCoroutine(SceneLoadCoroutine(sceneName)); } /// /// 场景加载+index /// public void SceneLoad() { StartCoroutine(SceneLoadCoroutine()); } /// /// 场景加载+SceneName(协程封装) /// /// public IEnumerator SceneLoadCoroutine(string sceneName) { if (SceneManager.GetActiveScene().name == "GetInitPos") { SceneManager.LoadScene("LoadingScene"); } yield return new WaitForSeconds(2f); AsyncLoadScene(sceneName); } /// /// 场景加载+index(协程封装) /// public IEnumerator SceneLoadCoroutine() { int index = SceneManager.GetActiveScene().buildIndex + 1; if (SceneManager.GetActiveScene().name == "GetInitPos" ) { SceneManager.LoadScene("LoadingScene"); } yield return new WaitForSeconds(2f); AsyncLoadScene(index); } /// /// 异步加载场景+index /// /// public void AsyncLoadScene(int index) { ao = SceneManager.LoadSceneAsync(index); ao.allowSceneActivation = false; } /// /// 异步加载场景+SceneName /// /// public void AsyncLoadScene(string sceneName) { ao = SceneManager.LoadSceneAsync(sceneName); ao.allowSceneActivation = false; } /// /// 加载过程 /// private void LoadingInProgress() { if(ao != null && ao.allowSceneActivation == false) { if (progress != null) { progress.fillAmount = ao.progress;// loading.text = (int)(ao.progress * 100) + "%";// } if (ao.progress >= 0.9f) { if (progress != null) { progress.fillAmount = 1;// loading.text = 100 + "%";// } //if (GameObject.Find("GameManager").GetComponent()) //{ // Destroy(GameObject.Find("GameManager").GetComponent()); //} ao.allowSceneActivation = true; SceneManager.sceneLoaded += StepAdd; } } } /// /// 切换场景结束回调/StepManager重新赋值 /// /// /// public void StepAdd(Scene s, LoadSceneMode sceneType) { //if (!GameObject.Find("GameManager").GetComponent()) //{ // GameObject.Find("GameManager").AddComponent(); //} ao = null; handle = null; } }