SceneBuildSettingsChecker.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Linq;
  4. /// <summary>
  5. /// Unity Editor script to check and fix Build Settings for scene loading
  6. /// </summary>
  7. public class SceneBuildSettingsChecker : MonoBehaviour
  8. {
  9. [Header("Scene Build Settings Checker")]
  10. [TextArea(3, 5)]
  11. public string instructions = "This script checks if required scenes are in Build Settings.\nUse the buttons below to check and fix issues.";
  12. [ContextMenu("Check Build Settings")]
  13. public void CheckBuildSettings()
  14. {
  15. Debug.Log("=== Checking Build Settings ===");
  16. #if UNITY_EDITOR
  17. // Get current build settings
  18. var scenes = EditorBuildSettings.scenes;
  19. Debug.Log($"Current scenes in Build Settings: {scenes.Length}");
  20. foreach (var scene in scenes)
  21. {
  22. Debug.Log($" - {scene.path} (enabled: {scene.enabled})");
  23. }
  24. // Check specific scenes we need
  25. string[] requiredScenes = { "TownSceen", "MapScene2" };
  26. foreach (string sceneName in requiredScenes)
  27. {
  28. bool found = scenes.Any(s => s.path.Contains(sceneName) && s.enabled);
  29. if (found)
  30. {
  31. Debug.Log($"✅ {sceneName} found in Build Settings");
  32. }
  33. else
  34. {
  35. Debug.LogError($"❌ {sceneName} NOT found in Build Settings!");
  36. Debug.LogError($" Add Assets/Scenes/{sceneName}.unity to Build Settings");
  37. }
  38. }
  39. Debug.Log("=== Build Settings Check Complete ===");
  40. Debug.Log("To fix: Go to File > Build Settings and drag scene files into the list");
  41. #else
  42. Debug.LogWarning("Build Settings can only be checked in Editor mode");
  43. #endif
  44. }
  45. [ContextMenu("Auto Fix Build Settings")]
  46. public void AutoFixBuildSettings()
  47. {
  48. #if UNITY_EDITOR
  49. Debug.Log("=== Auto-fixing Build Settings ===");
  50. // Define scenes that should be in build settings
  51. string[] sceneNames = {
  52. "TitleScreenScene",
  53. "MainTeamSelectScene",
  54. "MapScene2",
  55. "TownSceen",
  56. "BattleScene",
  57. "BattleSetupMenu"
  58. };
  59. var currentScenes = EditorBuildSettings.scenes.ToList();
  60. bool addedAny = false;
  61. foreach (string sceneName in sceneNames)
  62. {
  63. string scenePath = $"Assets/Scenes/{sceneName}.unity";
  64. // Check if scene already exists in build settings
  65. bool alreadyExists = currentScenes.Any(s => s.path == scenePath);
  66. if (!alreadyExists)
  67. {
  68. // Check if scene file exists
  69. if (System.IO.File.Exists(scenePath))
  70. {
  71. var newScene = new EditorBuildSettingsScene(scenePath, true);
  72. currentScenes.Add(newScene);
  73. addedAny = true;
  74. Debug.Log($"✅ Added {sceneName} to Build Settings");
  75. }
  76. else
  77. {
  78. Debug.LogWarning($"⚠️ Scene file not found: {scenePath}");
  79. }
  80. }
  81. else
  82. {
  83. Debug.Log($"✅ {sceneName} already in Build Settings");
  84. }
  85. }
  86. if (addedAny)
  87. {
  88. EditorBuildSettings.scenes = currentScenes.ToArray();
  89. Debug.Log("🎯 Build Settings updated successfully!");
  90. Debug.Log("Scenes are now ready for SceneManager.LoadScene()");
  91. }
  92. else
  93. {
  94. Debug.Log("ℹ️ No changes needed - all scenes already in Build Settings");
  95. }
  96. // Re-check to confirm
  97. CheckBuildSettings();
  98. #else
  99. Debug.LogWarning("Build Settings can only be modified in Editor mode");
  100. #endif
  101. }
  102. [ContextMenu("Test Scene Loading")]
  103. public void TestSceneLoading()
  104. {
  105. Debug.Log("=== Testing Scene Loading ===");
  106. // Test if we can load TownSceen
  107. string sceneName = "TownSceen";
  108. try
  109. {
  110. // This would actually load the scene, so we'll just check if it would work
  111. Debug.Log($"Attempting to load scene: {sceneName}");
  112. // Check Application.CanStreamedLevelBeLoaded for older Unity versions
  113. // or just try the load directly
  114. #if UNITY_EDITOR
  115. var scenes = UnityEditor.EditorBuildSettings.scenes;
  116. bool foundInBuild = scenes.Any(s => s.path.Contains(sceneName) && s.enabled);
  117. if (foundInBuild)
  118. {
  119. Debug.Log($"✅ Scene {sceneName} should load successfully");
  120. Debug.Log("You can now test entering settlements!");
  121. }
  122. else
  123. {
  124. Debug.LogError($"❌ Scene {sceneName} not properly configured in Build Settings");
  125. }
  126. #endif
  127. }
  128. catch (System.Exception e)
  129. {
  130. Debug.LogError($"❌ Scene loading test failed: {e.Message}");
  131. }
  132. }
  133. void OnGUI()
  134. {
  135. GUILayout.BeginArea(new Rect(10, Screen.height - 200, 300, 180));
  136. GUILayout.BeginVertical("box");
  137. GUILayout.Label("Scene Build Settings Fixer", new GUIStyle(GUI.skin.label) { fontSize = 12, fontStyle = FontStyle.Bold });
  138. if (GUILayout.Button("Check Build Settings"))
  139. {
  140. CheckBuildSettings();
  141. }
  142. if (GUILayout.Button("Auto Fix Build Settings"))
  143. {
  144. AutoFixBuildSettings();
  145. }
  146. if (GUILayout.Button("Test Scene Loading"))
  147. {
  148. TestSceneLoading();
  149. }
  150. GUILayout.Space(5);
  151. GUILayout.Label("Manual Fix:", new GUIStyle(GUI.skin.label) { fontSize = 10, fontStyle = FontStyle.Bold });
  152. GUILayout.Label("File > Build Settings", new GUIStyle(GUI.skin.label) { fontSize = 10 });
  153. GUILayout.Label("Drag scenes into list", new GUIStyle(GUI.skin.label) { fontSize = 10 });
  154. GUILayout.EndVertical();
  155. GUILayout.EndArea();
  156. }
  157. }