LoadingManager.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using UnityEngine.UI;
  4. using TMPro;
  5. using System.Collections;
  6. public class LoadingManager : MonoBehaviour
  7. {
  8. public static string sceneToLoad;
  9. [Header("UI")]
  10. public TMP_Text continueText;
  11. public Image progressFill;
  12. public float minimumLoadTime = 2f;
  13. public TMP_Text tipText;
  14. [Header("Tips")]
  15. [TextArea(2, 4)]
  16. public string[] tips;
  17. void Start()
  18. {
  19. StartCoroutine(LoadAsync());
  20. }
  21. void Update()
  22. {
  23. if (progressFill != null)
  24. progressFill.fillAmount = Mathf.PingPong(Time.time, 1f);
  25. }
  26. IEnumerator LoadAsync()
  27. {
  28. AsyncOperation operation =
  29. SceneManager.LoadSceneAsync(sceneToLoad);
  30. operation.allowSceneActivation = false;
  31. float displayedProgress = 0f;
  32. if (continueText != null)
  33. continueText.gameObject.SetActive(false);
  34. if (tips.Length > 0 && tipText != null)
  35. {
  36. int randomIndex = Random.Range(0, tips.Length);
  37. tipText.text = "Tip: \n" + tips[randomIndex];
  38. }
  39. while (!operation.isDone)
  40. {
  41. float targetProgress = Mathf.Clamp01(operation.progress / 0.9f);
  42. displayedProgress = Mathf.MoveTowards(
  43. displayedProgress,
  44. targetProgress,
  45. Time.deltaTime * 0.5f
  46. );
  47. if (progressFill != null)
  48. progressFill.fillAmount = displayedProgress;
  49. if (displayedProgress >= 1f)
  50. {
  51. continueText.gameObject.SetActive(true);
  52. if (Input.anyKeyDown)
  53. {
  54. operation.allowSceneActivation = true;
  55. }
  56. }
  57. yield return null;
  58. }
  59. }
  60. }