123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- 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;
- private SceneHandle handle;
- //测试
- private Image panel;
- private Image process;
- private Text loading;
- //测试
- private void Awake()
- {
- Instance = this;
- }
- private void Update()
- {
- if (/*SceneManager.GetActiveScene().name == "LoadingScene" && */process == null && loading == null)
- {
- //GameObject canvas = GameObject.Find("UIRoot").gameObject;
- //panel = canvas.transform.Find("LoadingPanel").GetComponent<Image>();
- //process = panel.transform.Find("Img_Loading/Img_LoadingBar").GetComponent<Image>();
- //loading = panel.transform.Find("Txt_Progress").GetComponent<Text>();
- }
- //LoadingInProgress();
- YooAssetLoadingProgress();
- }
- /// <summary>
- /// 通过YooAsset加载场景
- /// </summary>
- /// <param name="sceneName"></param>
- /// <returns></returns>
- public async UniTask SceneLoadByYooAsset(string sceneName)
- {
- await YooAssetManager.Instance.LoadScene(sceneName);
- handle = YooAssetManager.Instance.sceneLoading;
- }
- /// <summary>
- /// YooAsset加载过程
- /// </summary>
- void YooAssetLoadingProgress()
- {
- if (handle != null)
- {
- if (process != null)
- {
- process.fillAmount = handle.Progress+0.3f;//
- loading.text = (int)((handle.Progress+0.3f) * 100) + "%";//
- }
- if (handle.Progress >= 0.9f)
- {
- if (process != null)
- {
- process.fillAmount = 1;//
- loading.text = 100 + "%";//
- }
- //if (GameObject.Find("GameManager").GetComponent<StepManager>())
- //{
- // Destroy(GameObject.Find("GameManager").GetComponent<StepManager>());
- //}
- //ao.allowSceneActivation = true;
- handle.UnSuspend();
- UIManager.Instance.HideUI(WindowID.LoadingPanel);
- SceneManager.sceneLoaded += StepAdd;
- }
- }
- }
- /// <summary>
- /// 场景加载+SceneName
- /// </summary>
- /// <param name="sceneName"></param>
- public void SceneLoad(string sceneName)
- {
- StartCoroutine(SceneLoadCoroutine(sceneName));
- }
- /// <summary>
- /// 场景加载+index
- /// </summary>
- public void SceneLoad()
- {
- StartCoroutine(SceneLoadCoroutine());
- }
- /// <summary>
- /// 场景加载+SceneName(协程封装)
- /// </summary>
- /// <param name="sceneName"></param>
- public IEnumerator SceneLoadCoroutine(string sceneName)
- {
- if (SceneManager.GetActiveScene().name == "GetInitPos")
- {
- SceneManager.LoadScene("LoadingScene");
- }
- yield return new WaitForSeconds(2f);
- AsyncLoadScene(sceneName);
- }
- /// <summary>
- /// 场景加载+index(协程封装)
- /// </summary>
- public IEnumerator SceneLoadCoroutine()
- {
- int index = SceneManager.GetActiveScene().buildIndex + 1;
- if (SceneManager.GetActiveScene().name == "GetInitPos" )
- {
- SceneManager.LoadScene("LoadingScene");
- }
- yield return new WaitForSeconds(2f);
- AsyncLoadScene(index);
- }
- /// <summary>
- /// 异步加载场景+index
- /// </summary>
- /// <param name="sceneName"></param>
- public void AsyncLoadScene(int index)
- {
- ao = SceneManager.LoadSceneAsync(index);
- ao.allowSceneActivation = false;
- }
- /// <summary>
- /// 异步加载场景+SceneName
- /// </summary>
- /// <param name="sceneName"></param>
- public void AsyncLoadScene(string sceneName)
- {
- ao = SceneManager.LoadSceneAsync(sceneName);
- ao.allowSceneActivation = false;
- }
- /// <summary>
- /// 加载过程
- /// </summary>
- private void LoadingInProgress()
- {
- if(ao != null && ao.allowSceneActivation == false)
- {
- if (process != null)
- {
- process.fillAmount = ao.progress;//
- loading.text = (int)(ao.progress * 100) + "%";//
- }
- if (ao.progress >= 0.9f)
- {
- if (process != null)
- {
- process.fillAmount = 1;//
- loading.text = 100 + "%";//
- }
- //if (GameObject.Find("GameManager").GetComponent<StepManager>())
- //{
- // Destroy(GameObject.Find("GameManager").GetComponent<StepManager>());
- //}
- ao.allowSceneActivation = true;
- SceneManager.sceneLoaded += StepAdd;
- }
- }
-
- }
- /// <summary>
- /// 切换场景结束回调/StepManager重新赋值
- /// </summary>
- /// <param name="s"></param>
- /// <param name="sceneType"></param>
- public void StepAdd(Scene s, LoadSceneMode sceneType)
- {
- //if (!GameObject.Find("GameManager").GetComponent<StepManager>())
- //{
- // GameObject.Find("GameManager").AddComponent<StepManager>();
- //}
- ao = null;
- handle = null;
- }
- }
|