LoadProgressPanel.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using QFramework;
  6. using UnityEngine.SceneManagement;
  7. namespace QFramework
  8. {
  9. public class LoadProgressPanelData : UIPanelData
  10. {
  11. }
  12. public partial class LoadProgressPanel : UIPanel
  13. {
  14. private AsyncOperation m_AsyncOperation;
  15. private float m_CurrentProgress, m_TargetProgress;
  16. protected override void ProcessMsg(int eventId, QMsg msg)
  17. {
  18. throw new System.NotImplementedException();
  19. }
  20. protected override void OnInit(IUIData uiData = null)
  21. {
  22. mData = uiData as LoadProgressPanelData ?? new LoadProgressPanelData();
  23. // please add init code here
  24. }
  25. #region 内置函数
  26. protected override void OnOpen(IUIData uiData = null)
  27. {
  28. }
  29. protected override void OnShow()
  30. {
  31. }
  32. protected override void OnHide()
  33. {
  34. }
  35. protected override void OnClose()
  36. {
  37. }
  38. #endregion
  39. public void Init(string _sceneName)
  40. {
  41. Title.text = "加载中";
  42. LoadSlider.value = 0;
  43. m_CurrentProgress = m_TargetProgress = 0;
  44. StartCoroutine(LoadScene(_sceneName));
  45. }
  46. private IEnumerator LoadScene(string _sceneName)
  47. {
  48. m_AsyncOperation = SceneManager.LoadSceneAsync(_sceneName);
  49. m_AsyncOperation.allowSceneActivation = false;
  50. while (m_AsyncOperation.progress < 0.9f)
  51. {
  52. m_TargetProgress = (int)m_AsyncOperation.progress * 100;
  53. yield return LoadProgress();
  54. }
  55. m_TargetProgress = 100;
  56. yield return LoadProgress();
  57. Title.text = "加载完成";
  58. m_AsyncOperation.allowSceneActivation = true;
  59. }
  60. private IEnumerator LoadProgress()
  61. {
  62. while (m_CurrentProgress < m_TargetProgress)
  63. {
  64. ++m_CurrentProgress;
  65. LoadSlider.value = m_CurrentProgress / 100;
  66. yield return new WaitForEndOfFrame();
  67. }
  68. }
  69. }
  70. }