LoadManager.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. public class LoadManager : MonoBehaviour
  7. {
  8. // Start is called before the first frame update
  9. //public GameObject loadGame;
  10. public Scrollbar myScrollbar;
  11. //public Text text;
  12. void Start()
  13. {
  14. // 设置Scrollbar的值从0变到1
  15. StartCoroutine(AnimateScrollbar());
  16. }
  17. IEnumerator AnimateScrollbar()
  18. {
  19. // 设置Scrollbar的value从0开始
  20. myScrollbar.size = 0;
  21. // 计时5秒
  22. float duration = 5f;
  23. float elapsedTime = 0;
  24. while (elapsedTime < duration)
  25. {
  26. // 更新已过去的时间
  27. elapsedTime += Time.deltaTime;
  28. // 计算Scrollbar的新value值
  29. float newValue = Mathf.Clamp01(elapsedTime / duration);
  30. //text.text = Mathf.FloorToInt(newValue *100) + "%";
  31. // 更新Scrollbar的value
  32. myScrollbar.size = newValue;
  33. // 等待下一帧
  34. yield return null;
  35. }
  36. // 确保Scrollbar的value最终设置为1
  37. myScrollbar.size = 1;
  38. SceneManager.LoadScene("Home");
  39. }
  40. }