| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using UnityEngine;
- using UnityEngine.SceneManagement;
- /// <summary>
- /// Runtime scene loading tester - works in Play mode
- /// </summary>
- public class RuntimeSceneLoadTest : MonoBehaviour
- {
- [Header("Runtime Scene Load Test")]
- public string testSceneName = "TownSceen";
- [ContextMenu("Test Scene Load")]
- public void TestSceneLoad()
- {
- Debug.Log($"=== Testing Scene Load: {testSceneName} ===");
- try
- {
- // Check if scene is in build settings by trying to get its build index
- int buildIndex = SceneUtility.GetBuildIndexByScenePath($"Assets/Scenes/{testSceneName}.unity");
- if (buildIndex >= 0)
- {
- Debug.Log($"✅ Scene found in Build Settings at index {buildIndex}");
- Debug.Log($"Scene path: Assets/Scenes/{testSceneName}.unity");
- }
- else
- {
- Debug.LogError($"❌ Scene NOT found in Build Settings!");
- Debug.LogError($"Add 'Assets/Scenes/{testSceneName}.unity' to Build Settings");
- Debug.LogError("Go to: File > Build Settings > Add Open Scenes (or drag scene files)");
- return;
- }
- // Actually try to load the scene (this will happen)
- Debug.Log($"🚀 Loading scene: {testSceneName}");
- SceneManager.LoadScene(testSceneName);
- }
- catch (System.Exception e)
- {
- Debug.LogError($"❌ Scene loading failed: {e.Message}");
- Debug.LogError("Most likely cause: Scene not in Build Settings");
- }
- }
- [ContextMenu("List All Scenes in Build")]
- public void ListAllScenesInBuild()
- {
- Debug.Log("=== All Scenes in Build Settings ===");
- int sceneCount = SceneManager.sceneCountInBuildSettings;
- Debug.Log($"Total scenes in build: {sceneCount}");
- for (int i = 0; i < sceneCount; i++)
- {
- string scenePath = SceneUtility.GetScenePathByBuildIndex(i);
- string sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePath);
- Debug.Log($" [{i}] {sceneName} ({scenePath})");
- }
- if (sceneCount == 0)
- {
- Debug.LogError("❌ NO SCENES in Build Settings!");
- Debug.LogError("Go to File > Build Settings and add your scenes");
- }
- }
- void Update()
- {
- // Quick test keys
- if (Input.GetKeyDown(KeyCode.F12))
- {
- TestSceneLoad();
- }
- if (Input.GetKeyDown(KeyCode.F11))
- {
- ListAllScenesInBuild();
- }
- }
- void OnGUI()
- {
- GUILayout.BeginArea(new Rect(Screen.width - 250, Screen.height - 150, 240, 130));
- GUILayout.BeginVertical("box");
- GUILayout.Label("Scene Load Tester", new GUIStyle(GUI.skin.label) { fontSize = 11, fontStyle = FontStyle.Bold });
- if (GUILayout.Button($"Test Load {testSceneName} (F12)"))
- {
- TestSceneLoad();
- }
- if (GUILayout.Button("List Build Scenes (F11)"))
- {
- ListAllScenesInBuild();
- }
- GUILayout.Space(5);
- GUILayout.Label("If test fails:", new GUIStyle(GUI.skin.label) { fontSize = 9, fontStyle = FontStyle.Bold });
- GUILayout.Label("File > Build Settings", new GUIStyle(GUI.skin.label) { fontSize = 9 });
- GUILayout.Label("Add scenes to list", new GUIStyle(GUI.skin.label) { fontSize = 9 });
- GUILayout.EndVertical();
- GUILayout.EndArea();
- }
- }
|