GameManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. using System;
  7. public class GameManager : MonoBehaviour {
  8. public static GameManager instance;
  9. [SerializeField]
  10. private Fade fade;//遮罩
  11. [SerializeField]
  12. private Text loadText;//加载文字
  13. [SerializeField]
  14. private Image loadBar; //载入条
  15. [SerializeField]
  16. private String firstSceneName;
  17. [SerializeField]
  18. private Camera _loadingCamera;
  19. private bool isLodaingScene; // 是否处于加载Scene的过程中
  20. private void Awake()
  21. {
  22. if (instance) { Destroy(this.gameObject); }
  23. else
  24. {
  25. instance = this;
  26. }
  27. }
  28. private void Start()
  29. {
  30. StartCoroutine(ChangeScene(firstSceneName));
  31. }
  32. /// <summary>
  33. /// 加载并切换到新的场景
  34. /// </summary>
  35. /// <param name="sceneName">场景名字</param>
  36. public void SwitchScene(string sceneName)
  37. {
  38. StartCoroutine(ChangeScene(sceneName));
  39. }
  40. /// <summary>
  41. /// 加载场景的协程
  42. /// </summary>
  43. /// <param name="sceneName"></param>
  44. /// <returns></returns>
  45. private IEnumerator ChangeScene(string sceneName)
  46. {
  47. isLodaingScene = true;
  48. // 如果有别的场景在,就先淡出
  49. if (SceneManager.sceneCount >= 2)
  50. {
  51. fade.gameObject.SetActive(true);
  52. yield return fade.FadeOut();
  53. }
  54. // 先释放掉其他的场景
  55. while (SceneManager.sceneCount >= 2)
  56. {
  57. yield return SceneManager.UnloadSceneAsync(SceneManager.GetSceneAt(SceneManager.sceneCount - 1));
  58. }
  59. // 然后开始加载新场景
  60. yield return LoadSceneAsync(sceneName);
  61. isLodaingScene = false;
  62. // 加载完成后淡入
  63. yield return fade.FadeIn();
  64. fade.gameObject.SetActive(false);
  65. }
  66. /// <summary>
  67. /// 异步加载一个场景
  68. /// </summary>
  69. /// <param name="sceneName">需要加载的场景名</param>
  70. /// <returns></returns>
  71. private IEnumerator LoadSceneAsync(string sceneName)
  72. {
  73. int displayProgress = 0;
  74. int toProgress = 0;
  75. setLoadingEffct(true);
  76. AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
  77. asyncOperation.allowSceneActivation = false;//暂时不激活加载的场景
  78. //显示加载进度,将进度平缓显示
  79. while (asyncOperation.progress < 0.9f)
  80. {
  81. toProgress = (int)asyncOperation.progress * 100;
  82. while (displayProgress < toProgress)
  83. {
  84. ++displayProgress;
  85. SetLoadingPercentage(displayProgress);
  86. yield return new WaitForEndOfFrame();
  87. }
  88. }
  89. toProgress = 100;//将进度强行显示100,并平缓过度
  90. while (displayProgress < toProgress)
  91. {
  92. ++displayProgress;
  93. SetLoadingPercentage(displayProgress);
  94. yield return new WaitForEndOfFrame();
  95. }
  96. //激活加载场景
  97. asyncOperation.allowSceneActivation = true;
  98. yield return new WaitForSeconds(1f);//等待1秒钟
  99. yield return asyncOperation.isDone;//等待场景完成返回值
  100. //将场景设为活动场景
  101. SceneManager.SetActiveScene(SceneManager.GetSceneByName(sceneName));
  102. setLoadingEffct(false);
  103. }
  104. /// <summary>
  105. /// 显示加载值
  106. /// </summary>
  107. /// <param name="progress"></param>
  108. void SetLoadingPercentage(int progress)
  109. {
  110. loadText.text = progress + "%";
  111. loadBar.fillAmount = progress / 100f;
  112. }
  113. /// <summary>
  114. /// 开启与关闭加载文字和读条
  115. /// </summary>
  116. /// <param name="flag"></param>
  117. void setLoadingEffct(bool flag)
  118. {
  119. _loadingCamera.enabled = flag;
  120. _loadingCamera.GetComponent<AudioListener>().enabled = flag;
  121. loadText.enabled = flag;
  122. loadBar.enabled = flag;
  123. }
  124. }