LoadingManager.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. public static bool loadSave = false;
  18. void Start()
  19. {
  20. StartCoroutine(LoadAsync());
  21. }
  22. void Update()
  23. {
  24. if (progressFill != null)
  25. progressFill.fillAmount = Mathf.PingPong(Time.time, 1f);
  26. }
  27. IEnumerator LoadAsync()
  28. {
  29. AsyncOperation operation =
  30. SceneManager.LoadSceneAsync(sceneToLoad);
  31. operation.allowSceneActivation = false;
  32. float displayedProgress = 0f;
  33. if (continueText != null)
  34. continueText.gameObject.SetActive(false);
  35. // Show a random tip on-screen
  36. if (tips.Length > 0 && tipText != null)
  37. {
  38. int randomIndex = Random.Range(0, tips.Length);
  39. tipText.text = "Tip: \n" + tips[randomIndex];
  40. }
  41. // Minimum load time
  42. float timer = 0f;
  43. while (!operation.isDone)
  44. {
  45. timer += Time.deltaTime;
  46. // Smooth fill through bar
  47. float targetProgress = Mathf.Clamp01(operation.progress / 0.9f);
  48. displayedProgress = Mathf.MoveTowards(displayedProgress, targetProgress, Time.deltaTime * 0.5f);
  49. if (progressFill != null)
  50. progressFill.fillAmount = displayedProgress;
  51. // Show continue text when fully loaded
  52. if (displayedProgress >= 1f && timer >= minimumLoadTime)
  53. {
  54. if (continueText != null)
  55. continueText.gameObject.SetActive(true);
  56. // Wait for any key press
  57. if (Input.anyKeyDown)
  58. {
  59. operation.allowSceneActivation = true;
  60. }
  61. }
  62. yield return null;
  63. }
  64. // Wait one frame for GameManager to Awake
  65. yield return null;
  66. if (loadSave)
  67. {
  68. SaveData data = SaveSystem.Load(); // load JSON file
  69. if (data != null)
  70. {
  71. // Wait one frame to ensure GameManager is awake
  72. yield return null;
  73. if (GameManager.Instance != null)
  74. {
  75. GameManager.Instance.LoadGame(data);
  76. }
  77. else
  78. {
  79. Debug.LogWarning("No GameManager found! Starting new game.");
  80. }
  81. }
  82. else
  83. {
  84. Debug.LogWarning("No save file found! Starting new game.");
  85. GameManager.Instance.StartNewGame();
  86. }
  87. }
  88. else
  89. {
  90. GameManager.Instance.StartNewGame();
  91. }
  92. }
  93. public static SaveData currentSave;
  94. }