RuntimeSceneLoadTest.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. /// <summary>
  4. /// Runtime scene loading tester - works in Play mode
  5. /// </summary>
  6. public class RuntimeSceneLoadTest : MonoBehaviour
  7. {
  8. [Header("Runtime Scene Load Test")]
  9. public string testSceneName = "TownSceen";
  10. [ContextMenu("Test Scene Load")]
  11. public void TestSceneLoad()
  12. {
  13. Debug.Log($"=== Testing Scene Load: {testSceneName} ===");
  14. try
  15. {
  16. // Check if scene is in build settings by trying to get its build index
  17. int buildIndex = SceneUtility.GetBuildIndexByScenePath($"Assets/Scenes/{testSceneName}.unity");
  18. if (buildIndex >= 0)
  19. {
  20. Debug.Log($"✅ Scene found in Build Settings at index {buildIndex}");
  21. Debug.Log($"Scene path: Assets/Scenes/{testSceneName}.unity");
  22. }
  23. else
  24. {
  25. Debug.LogError($"❌ Scene NOT found in Build Settings!");
  26. Debug.LogError($"Add 'Assets/Scenes/{testSceneName}.unity' to Build Settings");
  27. Debug.LogError("Go to: File > Build Settings > Add Open Scenes (or drag scene files)");
  28. return;
  29. }
  30. // Actually try to load the scene (this will happen)
  31. Debug.Log($"🚀 Loading scene: {testSceneName}");
  32. SceneManager.LoadScene(testSceneName);
  33. }
  34. catch (System.Exception e)
  35. {
  36. Debug.LogError($"❌ Scene loading failed: {e.Message}");
  37. Debug.LogError("Most likely cause: Scene not in Build Settings");
  38. }
  39. }
  40. [ContextMenu("List All Scenes in Build")]
  41. public void ListAllScenesInBuild()
  42. {
  43. Debug.Log("=== All Scenes in Build Settings ===");
  44. int sceneCount = SceneManager.sceneCountInBuildSettings;
  45. Debug.Log($"Total scenes in build: {sceneCount}");
  46. for (int i = 0; i < sceneCount; i++)
  47. {
  48. string scenePath = SceneUtility.GetScenePathByBuildIndex(i);
  49. string sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePath);
  50. Debug.Log($" [{i}] {sceneName} ({scenePath})");
  51. }
  52. if (sceneCount == 0)
  53. {
  54. Debug.LogError("❌ NO SCENES in Build Settings!");
  55. Debug.LogError("Go to File > Build Settings and add your scenes");
  56. }
  57. }
  58. void Update()
  59. {
  60. // Quick test keys
  61. if (Input.GetKeyDown(KeyCode.F12))
  62. {
  63. TestSceneLoad();
  64. }
  65. if (Input.GetKeyDown(KeyCode.F11))
  66. {
  67. ListAllScenesInBuild();
  68. }
  69. }
  70. void OnGUI()
  71. {
  72. GUILayout.BeginArea(new Rect(Screen.width - 250, Screen.height - 150, 240, 130));
  73. GUILayout.BeginVertical("box");
  74. GUILayout.Label("Scene Load Tester", new GUIStyle(GUI.skin.label) { fontSize = 11, fontStyle = FontStyle.Bold });
  75. if (GUILayout.Button($"Test Load {testSceneName} (F12)"))
  76. {
  77. TestSceneLoad();
  78. }
  79. if (GUILayout.Button("List Build Scenes (F11)"))
  80. {
  81. ListAllScenesInBuild();
  82. }
  83. GUILayout.Space(5);
  84. GUILayout.Label("If test fails:", new GUIStyle(GUI.skin.label) { fontSize = 9, fontStyle = FontStyle.Bold });
  85. GUILayout.Label("File > Build Settings", new GUIStyle(GUI.skin.label) { fontSize = 9 });
  86. GUILayout.Label("Add scenes to list", new GUIStyle(GUI.skin.label) { fontSize = 9 });
  87. GUILayout.EndVertical();
  88. GUILayout.EndArea();
  89. }
  90. }