AsyncLoad.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Sirenix.OdinInspector;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. using UnityEngine.UI;
  7. public class AsyncLoad : MonoBehaviour
  8. {
  9. [Header("是否使用内置模式")]
  10. [SerializeField]
  11. private bool m_isUseBuiltinMode = true;
  12. [Header("基础熟悉设定")]
  13. [SerializeField]
  14. private Slider m_slider;
  15. /// <summary>
  16. /// 进度显示
  17. /// </summary>
  18. [SerializeField]
  19. private Text m_ProgressTxt;
  20. /// <summary>
  21. /// 文字显示
  22. /// </summary>
  23. [SerializeField]
  24. private Text m_txtDisplay;
  25. [SerializeField]
  26. private GameObject m_hintEffect;
  27. /// <summary>
  28. /// 加载速度
  29. /// </summary>
  30. [SerializeField]
  31. private float m_loadingSpeed = 1;
  32. /// <summary>
  33. /// 是否开始执行
  34. /// </summary>
  35. private bool m_isStart = false;
  36. /// <summary>
  37. /// 目标值
  38. /// </summary>
  39. private float m_targetValue;
  40. /// <summary>
  41. /// 当前进度
  42. /// </summary>
  43. private int m_currentProgress = 0;
  44. private AsyncOperation m_asyncOperation;
  45. /// <summary>
  46. /// 初始化
  47. /// </summary>
  48. /// <param name="_sceneName"></param>
  49. /// <param name="_isStart"></param>
  50. public void Init(string _sceneName, bool _isStart)
  51. {
  52. m_slider.value = 0;
  53. m_currentProgress = 0;
  54. m_isStart = false;
  55. if (m_isUseBuiltinMode)
  56. {
  57. if (_isStart)
  58. {
  59. StartCoroutine(AsyncLoadScene(_sceneName, _isStart));
  60. }
  61. }
  62. else
  63. {
  64. StartCoroutine(LoadProgress(_sceneName));
  65. }
  66. }
  67. #region 内置模式
  68. IEnumerator AsyncLoadScene(string _name, bool _isStart)
  69. {
  70. m_asyncOperation = SceneManager.LoadSceneAsync(_name);
  71. m_asyncOperation.allowSceneActivation = false;
  72. m_isStart = _isStart;
  73. yield return m_asyncOperation;
  74. }
  75. void FixedUpdate()
  76. {
  77. if (m_isStart)
  78. {
  79. Debug.Log("进入");
  80. m_targetValue = m_asyncOperation.progress;
  81. if (m_asyncOperation.progress >= 0.9f)
  82. {
  83. m_targetValue = 1.0f;
  84. }
  85. if (m_targetValue != m_slider.value)
  86. {
  87. m_slider.value = Mathf.Lerp(m_slider.value, m_targetValue, Time.deltaTime * m_loadingSpeed);
  88. if (Mathf.Abs(m_slider.value - m_targetValue) < 0.01f)
  89. {
  90. m_slider.value = m_targetValue;
  91. }
  92. }
  93. m_ProgressTxt.text = ((int)(m_slider.value * 100)).ToString() + "%";
  94. if ((int)(m_slider.value * 100) == 100)
  95. {
  96. m_isStart = false;
  97. m_asyncOperation.allowSceneActivation = true;
  98. }
  99. }
  100. }
  101. #endregion
  102. /// <summary>
  103. /// 平滑加载
  104. /// </summary>
  105. /// <param name="_name"></param>
  106. private IEnumerator LoadProgress(string _name)
  107. {
  108. while (m_slider.value < 1f)
  109. {
  110. m_targetValue += m_loadingSpeed * Time.deltaTime;
  111. m_ProgressTxt.text = (int)(m_targetValue * 100) + "%";
  112. m_slider.value = Mathf.Clamp01(m_targetValue);
  113. yield return null;
  114. }
  115. if (m_slider.value == 1)
  116. {
  117. SetSubsequentOperations();
  118. SceneManager.LoadSceneAsync(_name);
  119. }
  120. }
  121. /// <summary>
  122. /// 设置模型状态
  123. /// </summary>
  124. private void SetSubsequentOperations()
  125. {
  126. m_txtDisplay.text = "资源加载完成";
  127. m_hintEffect.SetActive(false);
  128. }
  129. }