SceneNavigationManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System.Collections;
  4. public class SceneNavigationManager : MonoBehaviour
  5. {
  6. public static SceneNavigationManager Instance { get; private set; }
  7. [Header("Scene Transition")]
  8. public bool useLoadingScreen = false;
  9. public float minimumLoadingTime = 1f;
  10. // Scene name constants for easy reference
  11. public const string TITLE_SCENE = "TitleScreen";
  12. public const string TEAM_SELECT_SCENE = "MainTeamSelectScene";
  13. public const string BATTLE_SETUP_SCENE = "BattleSetupMenu";
  14. public const string BATTLE_SCENE = "BattleScene";
  15. public const string MAP_SCENE = "MapScene";
  16. private void Awake()
  17. {
  18. if (Instance == null)
  19. {
  20. Instance = this;
  21. DontDestroyOnLoad(gameObject);
  22. }
  23. else
  24. {
  25. Destroy(gameObject);
  26. }
  27. }
  28. #region Public Scene Navigation Methods
  29. public void GoToTitleScreen()
  30. {
  31. LoadScene(TITLE_SCENE);
  32. }
  33. public void GoToTeamSelect()
  34. {
  35. LoadScene(TEAM_SELECT_SCENE);
  36. }
  37. public void GoToBattleSetup()
  38. {
  39. LoadScene(BATTLE_SETUP_SCENE);
  40. }
  41. public void GoToBattle()
  42. {
  43. LoadScene(BATTLE_SCENE);
  44. }
  45. public void GoToMapScene()
  46. {
  47. LoadScene(MAP_SCENE);
  48. }
  49. #endregion
  50. #region Core Scene Loading
  51. public void LoadScene(string sceneName)
  52. {
  53. if (string.IsNullOrEmpty(sceneName))
  54. {
  55. Debug.LogError("Scene name is null or empty!");
  56. return;
  57. }
  58. if (useLoadingScreen)
  59. {
  60. StartCoroutine(LoadSceneWithLoadingScreen(sceneName));
  61. }
  62. else
  63. {
  64. SceneManager.LoadScene(sceneName);
  65. }
  66. }
  67. public void LoadSceneAsync(string sceneName, System.Action onComplete = null)
  68. {
  69. StartCoroutine(LoadSceneAsyncCoroutine(sceneName, onComplete));
  70. }
  71. private IEnumerator LoadSceneWithLoadingScreen(string sceneName)
  72. {
  73. // TODO: Show loading screen UI here
  74. yield return new WaitForSeconds(0.1f); // Brief delay to show loading screen
  75. float startTime = Time.time;
  76. // Start loading the scene asynchronously
  77. AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);
  78. asyncLoad.allowSceneActivation = false;
  79. // Wait for the scene to be ready or minimum loading time
  80. while (!asyncLoad.isDone)
  81. {
  82. float progress = Mathf.Clamp01(asyncLoad.progress / 0.9f);
  83. // TODO: Update loading screen progress here
  84. // If loading is done and minimum time has passed, activate the scene
  85. if (asyncLoad.progress >= 0.9f && (Time.time - startTime) >= minimumLoadingTime)
  86. {
  87. asyncLoad.allowSceneActivation = true;
  88. }
  89. yield return null;
  90. }
  91. // TODO: Hide loading screen UI here
  92. }
  93. private IEnumerator LoadSceneAsyncCoroutine(string sceneName, System.Action onComplete)
  94. {
  95. AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);
  96. while (!asyncLoad.isDone)
  97. {
  98. yield return null;
  99. }
  100. onComplete?.Invoke();
  101. }
  102. #endregion
  103. #region Scene Validation
  104. public bool IsSceneInBuildSettings(string sceneName)
  105. {
  106. for (int i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
  107. {
  108. string scenePath = SceneUtility.GetScenePathByBuildIndex(i);
  109. string sceneNameFromPath = System.IO.Path.GetFileNameWithoutExtension(scenePath);
  110. if (sceneNameFromPath.Equals(sceneName, System.StringComparison.OrdinalIgnoreCase))
  111. {
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. public string GetCurrentSceneName()
  118. {
  119. return SceneManager.GetActiveScene().name;
  120. }
  121. #endregion
  122. #region Development/Debug Methods
  123. [System.Diagnostics.Conditional("UNITY_EDITOR")]
  124. public void LogAllScenesInBuild()
  125. {
  126. for (int i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
  127. {
  128. string scenePath = SceneUtility.GetScenePathByBuildIndex(i);
  129. string sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePath);
  130. }
  131. }
  132. #endregion
  133. }