AsyncLoad_Single.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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_Single: 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. public string m_SceneName;
  46. /// <summary>
  47. /// 初始化
  48. /// </summary>
  49. /// <param name="_sceneName"></param>
  50. /// <param name="_isStart"></param>
  51. public void Init(bool _isStart)
  52. {
  53. m_slider.value = 0;
  54. m_currentProgress = 0;
  55. m_isStart = false;
  56. if (m_isUseBuiltinMode)
  57. {
  58. if (_isStart)
  59. {
  60. StartCoroutine(AsyncLoadScene(m_SceneName, _isStart));
  61. }
  62. }
  63. else
  64. {
  65. StartCoroutine(LoadProgress(m_SceneName));
  66. }
  67. }
  68. #region 内置模式
  69. IEnumerator AsyncLoadScene(string _name,bool _isStart)
  70. {
  71. m_asyncOperation = SceneManager.LoadSceneAsync(_name);
  72. m_asyncOperation.allowSceneActivation = false;
  73. m_isStart = _isStart;
  74. yield return m_asyncOperation;
  75. }
  76. void FixedUpdate()
  77. {
  78. if (m_isStart)
  79. {
  80. Debug.Log("进入");
  81. m_targetValue = m_asyncOperation.progress;
  82. if (m_asyncOperation.progress >= 0.9f)
  83. {
  84. m_targetValue = 1.0f;
  85. }
  86. if (m_targetValue != m_slider.value)
  87. {
  88. m_slider.value = Mathf.Lerp(m_slider.value, m_targetValue, Time.deltaTime * m_loadingSpeed);
  89. if (Mathf.Abs(m_slider.value - m_targetValue) < 0.01f)
  90. {
  91. m_slider.value = m_targetValue;
  92. }
  93. }
  94. m_ProgressTxt.text = ((int)(m_slider.value * 100)).ToString() + "%";
  95. if ((int)(m_slider.value * 100) == 100)
  96. {
  97. m_isStart = false;
  98. m_asyncOperation.allowSceneActivation = true;
  99. }
  100. }
  101. }
  102. #endregion
  103. /// <summary>
  104. /// 平滑加载
  105. /// </summary>
  106. /// <param name="_name"></param>
  107. private IEnumerator LoadProgress(string _name)
  108. {
  109. while (m_slider.value < 1f)
  110. {
  111. m_targetValue += m_loadingSpeed * Time.deltaTime;
  112. m_ProgressTxt.text = (int)(m_targetValue * 100) + "%";
  113. m_slider.value = Mathf.Clamp01(m_targetValue);
  114. yield return null;
  115. }
  116. if (m_slider.value == 1)
  117. {
  118. SetSubsequentOperations();
  119. SceneManager.LoadSceneAsync(_name);
  120. }
  121. }
  122. /// <summary>
  123. /// 设置模型状态
  124. /// </summary>
  125. private void SetSubsequentOperations()
  126. {
  127. m_txtDisplay.text = "资源加载完成";
  128. m_hintEffect.SetActive(false);
  129. }
  130. }