ScenesManager.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. private SceneHandle handle;
  14. //测试
  15. private Image panel;
  16. private Image process;
  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" && */process == null && loading == null)
  26. {
  27. //GameObject canvas = GameObject.Find("UIRoot").gameObject;
  28. //panel = canvas.transform.Find("LoadingPanel").GetComponent<Image>();
  29. //process = panel.transform.Find("Img_Loading/Img_LoadingBar").GetComponent<Image>();
  30. //loading = panel.transform.Find("Txt_Progress").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. handle = YooAssetManager.Instance.sceneLoading;
  44. }
  45. /// <summary>
  46. /// YooAsset加载过程
  47. /// </summary>
  48. void YooAssetLoadingProgress()
  49. {
  50. if (handle != null)
  51. {
  52. if (process != null)
  53. {
  54. process.fillAmount = handle.Progress+0.3f;//
  55. loading.text = (int)((handle.Progress+0.3f) * 100) + "%";//
  56. }
  57. if (handle.Progress >= 0.9f)
  58. {
  59. if (process != null)
  60. {
  61. process.fillAmount = 1;//
  62. loading.text = 100 + "%";//
  63. }
  64. //if (GameObject.Find("GameManager").GetComponent<StepManager>())
  65. //{
  66. // Destroy(GameObject.Find("GameManager").GetComponent<StepManager>());
  67. //}
  68. //ao.allowSceneActivation = true;
  69. handle.UnSuspend();
  70. UIManager.Instance.HideUI(WindowID.LoadingPanel);
  71. SceneManager.sceneLoaded += StepAdd;
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// 场景加载+SceneName
  77. /// </summary>
  78. /// <param name="sceneName"></param>
  79. public void SceneLoad(string sceneName)
  80. {
  81. StartCoroutine(SceneLoadCoroutine(sceneName));
  82. }
  83. /// <summary>
  84. /// 场景加载+index
  85. /// </summary>
  86. public void SceneLoad()
  87. {
  88. StartCoroutine(SceneLoadCoroutine());
  89. }
  90. /// <summary>
  91. /// 场景加载+SceneName(协程封装)
  92. /// </summary>
  93. /// <param name="sceneName"></param>
  94. public IEnumerator SceneLoadCoroutine(string sceneName)
  95. {
  96. if (SceneManager.GetActiveScene().name == "GetInitPos")
  97. {
  98. SceneManager.LoadScene("LoadingScene");
  99. }
  100. yield return new WaitForSeconds(2f);
  101. AsyncLoadScene(sceneName);
  102. }
  103. /// <summary>
  104. /// 场景加载+index(协程封装)
  105. /// </summary>
  106. public IEnumerator SceneLoadCoroutine()
  107. {
  108. int index = SceneManager.GetActiveScene().buildIndex + 1;
  109. if (SceneManager.GetActiveScene().name == "GetInitPos" )
  110. {
  111. SceneManager.LoadScene("LoadingScene");
  112. }
  113. yield return new WaitForSeconds(2f);
  114. AsyncLoadScene(index);
  115. }
  116. /// <summary>
  117. /// 异步加载场景+index
  118. /// </summary>
  119. /// <param name="sceneName"></param>
  120. public void AsyncLoadScene(int index)
  121. {
  122. ao = SceneManager.LoadSceneAsync(index);
  123. ao.allowSceneActivation = false;
  124. }
  125. /// <summary>
  126. /// 异步加载场景+SceneName
  127. /// </summary>
  128. /// <param name="sceneName"></param>
  129. public void AsyncLoadScene(string sceneName)
  130. {
  131. ao = SceneManager.LoadSceneAsync(sceneName);
  132. ao.allowSceneActivation = false;
  133. }
  134. /// <summary>
  135. /// 加载过程
  136. /// </summary>
  137. private void LoadingInProgress()
  138. {
  139. if(ao != null && ao.allowSceneActivation == false)
  140. {
  141. if (process != null)
  142. {
  143. process.fillAmount = ao.progress;//
  144. loading.text = (int)(ao.progress * 100) + "%";//
  145. }
  146. if (ao.progress >= 0.9f)
  147. {
  148. if (process != null)
  149. {
  150. process.fillAmount = 1;//
  151. loading.text = 100 + "%";//
  152. }
  153. //if (GameObject.Find("GameManager").GetComponent<StepManager>())
  154. //{
  155. // Destroy(GameObject.Find("GameManager").GetComponent<StepManager>());
  156. //}
  157. ao.allowSceneActivation = true;
  158. SceneManager.sceneLoaded += StepAdd;
  159. }
  160. }
  161. }
  162. /// <summary>
  163. /// 切换场景结束回调/StepManager重新赋值
  164. /// </summary>
  165. /// <param name="s"></param>
  166. /// <param name="sceneType"></param>
  167. public void StepAdd(Scene s, LoadSceneMode sceneType)
  168. {
  169. //if (!GameObject.Find("GameManager").GetComponent<StepManager>())
  170. //{
  171. // GameObject.Find("GameManager").AddComponent<StepManager>();
  172. //}
  173. ao = null;
  174. handle = null;
  175. }
  176. }