ScenesManager.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using Cysharp.Threading.Tasks;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. using UnityEngine.UI;
  8. using YooAsset;
  9. public class ScenesManager : MonoBehaviour
  10. {
  11. public static ScenesManager Instance { get; private set; }
  12. private AsyncOperation ao;
  13. public SceneHandle handle;
  14. //测试
  15. private Image panel;
  16. private Image progress;
  17. private Text loading;
  18. //测试
  19. private void Awake()
  20. {
  21. Instance = this;
  22. }
  23. private void Update()
  24. {
  25. if (/*SceneManager.GetActiveScene().name == "LoadingScene" && */progress == null && loading == null)
  26. {
  27. GameObject canvas = GameObject.Find("UIRoot").gameObject;
  28. panel = canvas.transform.Find("LoadingPanel").GetComponent<Image>();
  29. progress = panel.transform.Find("LoadingBar/LoadingProgress").GetComponent<Image>();
  30. loading = panel.transform.Find("ProgressTxt").GetComponent<Text>();
  31. }
  32. //LoadingInProgress();
  33. YooAssetLoadingProgress();
  34. }
  35. /// <summary>
  36. /// 通过YooAsset加载场景
  37. /// </summary>
  38. /// <param name="sceneName"></param>
  39. /// <returns></returns>
  40. public async UniTask SceneLoadByYooAsset(string sceneName)
  41. {
  42. await YooAssetManager.Instance.LoadScene(sceneName);
  43. }
  44. /// <summary>
  45. /// YooAsset加载过程
  46. /// </summary>
  47. void YooAssetLoadingProgress()
  48. {
  49. if (handle != null)
  50. {
  51. //Debug.Log(handle.Progress);
  52. if (progress != null)
  53. {
  54. progress.fillAmount = handle.Progress;//
  55. loading.text = (int)((handle.Progress) * 100) + "%";//
  56. }
  57. if (handle.Progress >= 0.9f)
  58. {
  59. if (progress != null)
  60. {
  61. progress.fillAmount = 1;//
  62. loading.text = 100 + "%";//
  63. }
  64. handle.UnSuspend();
  65. UIManager.Instance.HideUI(WindowID.LoadingPanel);
  66. SceneManager.sceneLoaded += StepAdd;
  67. }
  68. }
  69. }
  70. /// <summary>
  71. /// 场景加载+SceneName
  72. /// </summary>
  73. /// <param name="sceneName"></param>
  74. public void SceneLoad(string sceneName)
  75. {
  76. StartCoroutine(SceneLoadCoroutine(sceneName));
  77. }
  78. /// <summary>
  79. /// 场景加载+index
  80. /// </summary>
  81. public void SceneLoad()
  82. {
  83. StartCoroutine(SceneLoadCoroutine());
  84. }
  85. /// <summary>
  86. /// 场景加载+SceneName(协程封装)
  87. /// </summary>
  88. /// <param name="sceneName"></param>
  89. public IEnumerator SceneLoadCoroutine(string sceneName)
  90. {
  91. if (SceneManager.GetActiveScene().name == "GetInitPos")
  92. {
  93. SceneManager.LoadScene("LoadingScene");
  94. }
  95. yield return new WaitForSeconds(2f);
  96. AsyncLoadScene(sceneName);
  97. }
  98. /// <summary>
  99. /// 场景加载+index(协程封装)
  100. /// </summary>
  101. public IEnumerator SceneLoadCoroutine()
  102. {
  103. int index = SceneManager.GetActiveScene().buildIndex + 1;
  104. if (SceneManager.GetActiveScene().name == "GetInitPos" )
  105. {
  106. SceneManager.LoadScene("LoadingScene");
  107. }
  108. yield return new WaitForSeconds(2f);
  109. AsyncLoadScene(index);
  110. }
  111. /// <summary>
  112. /// 异步加载场景+index
  113. /// </summary>
  114. /// <param name="sceneName"></param>
  115. public void AsyncLoadScene(int index)
  116. {
  117. ao = SceneManager.LoadSceneAsync(index);
  118. ao.allowSceneActivation = false;
  119. }
  120. /// <summary>
  121. /// 异步加载场景+SceneName
  122. /// </summary>
  123. /// <param name="sceneName"></param>
  124. public void AsyncLoadScene(string sceneName)
  125. {
  126. ao = SceneManager.LoadSceneAsync(sceneName);
  127. ao.allowSceneActivation = false;
  128. }
  129. /// <summary>
  130. /// 加载过程
  131. /// </summary>
  132. private void LoadingInProgress()
  133. {
  134. if(ao != null && ao.allowSceneActivation == false)
  135. {
  136. if (progress != null)
  137. {
  138. progress.fillAmount = ao.progress;//
  139. loading.text = (int)(ao.progress * 100) + "%";//
  140. }
  141. if (ao.progress >= 0.9f)
  142. {
  143. if (progress != null)
  144. {
  145. progress.fillAmount = 1;//
  146. loading.text = 100 + "%";//
  147. }
  148. //if (GameObject.Find("GameManager").GetComponent<StepManager>())
  149. //{
  150. // Destroy(GameObject.Find("GameManager").GetComponent<StepManager>());
  151. //}
  152. ao.allowSceneActivation = true;
  153. SceneManager.sceneLoaded += StepAdd;
  154. }
  155. }
  156. }
  157. /// <summary>
  158. /// 切换场景结束回调/StepManager重新赋值
  159. /// </summary>
  160. /// <param name="s"></param>
  161. /// <param name="sceneType"></param>
  162. public void StepAdd(Scene s, LoadSceneMode sceneType)
  163. {
  164. //if (!GameObject.Find("GameManager").GetComponent<StepManager>())
  165. //{
  166. // GameObject.Find("GameManager").AddComponent<StepManager>();
  167. //}
  168. ao = null;
  169. handle = null;
  170. }
  171. }