当前位置:   article > 正文

unity场景异步加载(进度条缓慢加载)_unity 进度条加载速度由慢到快

unity 进度条加载速度由慢到快
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. enum Phase3 { Start, Middle, End }
  7. public class loading : MonoBehaviour
  8. {
  9. static AsyncOperation async = null;
  10. public Text text;
  11. public Image Loading;
  12. [Tooltip("下个场景的名字,在Build Settings发布设置中需要先将该Scene加载进来才能识别!")]
  13. public string Scene_name;
  14. public Image float_tips;
  15. public float start_positon;
  16. public float end_positon;
  17. private Phase3 _phase = Phase3.Start;
  18. private float _progressValue = 0;
  19. private float START_LIMITER = 0.1f;
  20. private float _endStart = 0;
  21. private float _endValue = 0;
  22. private float MIDDLE_LIMITER = 1.0f;
  23. private float _middleValue = 0;
  24. //void Awake()
  25. // {
  26. // Screen.SetResolution(1920,1080,true);
  27. // }
  28. void Start ()
  29. {
  30. StartCoroutine(LoadScene());
  31. }
  32. // Update is called once per frame
  33. void Update ()
  34. {
  35. if (text && Loading && async != null)
  36. {
  37. if (this._phase == Phase3.Start)
  38. {
  39. // 正常
  40. }
  41. else if (this._phase == Phase3.Middle)
  42. {
  43. // 1秒加1个单位
  44. this.middle(Time.deltaTime);
  45. }
  46. else
  47. {
  48. // 1秒内从当前位置跳到结束
  49. this.end(Time.deltaTime);
  50. }
  51. text.text = ( (int)( _progressValue * 100 ) ).ToString() + "%";
  52. Loading.fillAmount = _progressValue;
  53. float_tips.GetComponent<RectTransform>().localPosition = new Vector3(start_positon + end_positon * _progressValue, float_tips.GetComponent<RectTransform>().localPosition.y, 0);
  54. //temp = Mathf.Lerp(temp, async.progress, Time.deltaTime);
  55. //text.text = ( (int)( temp * 100 * 10 / 9 ) ).ToString() + "%";
  56. //Loading.fillAmount = temp * 10 / 9;
  57. if (this._progressValue >= 1)
  58. {
  59. async.allowSceneActivation = true;
  60. }
  61. //Debug.Log(timer);
  62. //if (temp / 9 * 10 >= 0.95)
  63. //{
  64. // text.text = 100.ToString() + "%";
  65. // Loading.fillAmount = 1;
  66. // asyn.allowSceneActivation = true;
  67. //}
  68. }
  69. }
  70. private void middle (float delta_time)
  71. {
  72. this._middleValue += delta_time;
  73. if (this._middleValue >= MIDDLE_LIMITER)
  74. {
  75. this._middleValue -= MIDDLE_LIMITER;
  76. if (this._progressValue < 0.99f)
  77. {
  78. this._progressValue += 0.01f;
  79. }
  80. }
  81. }
  82. private void end (float delta_time)
  83. {
  84. this._endValue += delta_time;
  85. this._progressValue = Mathf.Lerp(this._endStart, 1, this._endValue);
  86. }
  87. IEnumerator LoadScene ()
  88. {
  89. //异步读取场景。
  90. async = SceneManager.LoadSceneAsync(Scene_name);
  91. async.allowSceneActivation = false;
  92. while (async.progress < 0.9f)
  93. {
  94. Debug.Log(async.progress);
  95. //Debug.Log(async.progress + "----" + (async.progress <= 0.9f));
  96. if (async.progress <= START_LIMITER)
  97. {
  98. this._progressValue = async.progress;
  99. }
  100. else
  101. {
  102. this._phase = Phase3.Middle;
  103. }
  104. yield return null;
  105. }
  106. this._phase = Phase3.End;
  107. this._endStart = this._progressValue;
  108. //while (_progressValue < 1)
  109. //{
  110. // yield return new WaitForEndOfFrame();
  111. //}
  112. //Debug.LogWarning(_progressValue);
  113. yield return async;
  114. }
  115. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/125269
推荐阅读
相关标签
  

闽ICP备14008679号