SceneController.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. using System;
  7. [Serializable]
  8. public class SceneBGM
  9. {
  10. public string Name;
  11. public AudioClip clip;
  12. }
  13. public class SceneController : MonoBehaviour
  14. {
  15. private static SceneController instance;
  16. public static SceneController Instacne
  17. {
  18. get
  19. {
  20. if(!instance)
  21. {
  22. instance = FindObjectOfType<SceneController>();
  23. }
  24. if(!instance)
  25. {
  26. Debug.Log("场景控制物体不存在!");
  27. }
  28. return instance;
  29. }
  30. }
  31. public bool IsLoadingScene
  32. {
  33. get
  34. {
  35. return isLodaingScene;
  36. }
  37. }
  38. [SerializeField]
  39. private Fade fade;//幕布
  40. [SerializeField]
  41. private Text loadText;//加载文本
  42. [SerializeField]
  43. private String firstSceneName;//初始场景名
  44. [SerializeField]
  45. private GameObject loadAnim;//加载动画
  46. [SerializeField]
  47. private Text loadUI;//显示加载进度
  48. [SerializeField,Header("背景音乐")]
  49. List<SceneBGM> BGMS;//所有BGM的存放
  50. //private int BGMIndex;//现在需要播放的BGM指针
  51. AudioSource BGMaudio;//BGM播放器
  52. private bool isLodaingScene; // 是否处于加载Scene的过程中
  53. private void Awake()
  54. {
  55. BGMaudio = GetComponent<AudioSource>();
  56. }
  57. private void Start()
  58. {
  59. StartCoroutine(ChangeScene(firstSceneName));
  60. loadText.text = "Please Wait";
  61. }
  62. /// <summary>
  63. /// 加载并切换到新的场景
  64. /// </summary>
  65. /// <param name="sceneName">场景名字</param>
  66. public void SwitchScene(string sceneName)
  67. {
  68. StartCoroutine(ChangeScene(sceneName));
  69. }
  70. /// <summary>
  71. /// 加载场景的协程
  72. /// </summary>
  73. /// <param name="sceneName"></param>
  74. /// <returns></returns>
  75. private IEnumerator ChangeScene(string sceneName)
  76. {
  77. isLodaingScene = true;
  78. // 如果有别的场景在,就先淡出
  79. if (SceneManager.sceneCount >= 2)
  80. {
  81. yield return fade.FadeOut();
  82. }
  83. // 先释放掉其他的场景
  84. while(SceneManager.sceneCount >= 2)
  85. {
  86. yield return SceneManager.UnloadSceneAsync(SceneManager.GetSceneAt(SceneManager.sceneCount - 1));
  87. }
  88. // 然后开始加载新场景
  89. yield return LoadSceneAsync(sceneName);
  90. //切换背景音乐BGM
  91. BGMChange(sceneName);
  92. isLodaingScene = false;
  93. // 加载完成后淡入
  94. yield return fade.FadeIn();
  95. }
  96. /// <summary>
  97. /// 异步加载一个场景
  98. /// </summary>
  99. /// <param name="sceneName">需要加载的场景名</param>
  100. /// <returns></returns>
  101. private IEnumerator LoadSceneAsync(string sceneName)
  102. {
  103. int currProgress = 0;
  104. int showProgress = 0;
  105. loadText.enabled = true;
  106. loadAnim.SetActive(true);
  107. loadUI.enabled = true;
  108. AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
  109. async.allowSceneActivation = false;
  110. while (async.progress<0.9f)
  111. {
  112. currProgress = (int)(async.progress * 100);
  113. while (showProgress < currProgress)
  114. {
  115. showProgress++;
  116. setProgressValue(showProgress);
  117. yield return new WaitForEndOfFrame(); //等待一帧
  118. }
  119. //loadText.text = "NowLoading: " + (asyncOperation.progress * 100).ToString("f1") + "%";
  120. yield return null;
  121. }
  122. currProgress = 100;
  123. while (showProgress < currProgress)
  124. {
  125. showProgress++;
  126. setProgressValue(showProgress);
  127. yield return new WaitForEndOfFrame(); //等待一帧
  128. }
  129. async.allowSceneActivation = true;
  130. yield return async.isDone;
  131. SceneManager.SetActiveScene(SceneManager.GetSceneAt(SceneManager.sceneCount - 1));
  132. loadAnim.SetActive(false);
  133. loadText.enabled = false;
  134. loadUI.enabled = false;
  135. }
  136. void setProgressValue(int value)
  137. {
  138. loadUI.text = value + "%";
  139. }
  140. /// <summary>
  141. /// 切换背景音乐
  142. /// </summary>
  143. /// <param name="name"></param>
  144. void BGMChange(string name)
  145. {
  146. for (int i = 0; i < BGMS.Count; i++)
  147. {
  148. if (BGMS[i].Name == name)
  149. {
  150. BGMaudio.clip = BGMS[i].clip;
  151. BGMaudio.Play();
  152. BGMaudio.loop = true;
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// BGM 音量
  158. /// </summary>
  159. /// <param name="volume">音量</param>
  160. public void BGMvolume(float volume)
  161. {
  162. BGMaudio.volume = volume;
  163. }
  164. }