MapSceneSetup.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. /// <summary>
  4. /// Setup script to configure all UI systems in the map scene.
  5. /// Handles both travel system and team overview setup automatically.
  6. /// </summary>
  7. public class MapSceneSetup : MonoBehaviour
  8. {
  9. [Header("Setup Options")]
  10. public bool autoSetupOnStart = true;
  11. public bool showDebugLogs = false;
  12. [Header("UI Settings")]
  13. public bool enableTeamOverview = true;
  14. public bool enableTravelSystem = true;
  15. void Start()
  16. {
  17. if (autoSetupOnStart)
  18. {
  19. SetupMapScene();
  20. }
  21. }
  22. [ContextMenu("Setup Map Scene")]
  23. public void SetupMapScene()
  24. {
  25. // Check if we're in Play Mode
  26. if (!Application.isPlaying)
  27. {
  28. Debug.LogWarning("⚠️ MapSceneSetup: Please run this setup during Play Mode, not Edit Mode!");
  29. Debug.LogWarning("💡 Either enter Play Mode and try again, or let it run automatically via autoSetupOnStart.");
  30. return;
  31. }
  32. if (showDebugLogs)
  33. {
  34. Debug.Log("🏗️ Setting up Map Scene UI systems...");
  35. }
  36. // Setup team overview if enabled
  37. if (enableTeamOverview)
  38. {
  39. SetupTeamOverview();
  40. }
  41. // Setup travel system if enabled
  42. if (enableTravelSystem)
  43. {
  44. SetupTravelSystem();
  45. }
  46. // Setup quest system
  47. SetupQuestSystem();
  48. if (showDebugLogs)
  49. {
  50. Debug.Log("✅ Map Scene setup complete!");
  51. }
  52. }
  53. [ContextMenu("Force Setup (Play Mode Only)")]
  54. public void ForceSetupInPlayMode()
  55. {
  56. if (!Application.isPlaying)
  57. {
  58. Debug.LogError("❌ This setup requires Play Mode! Enter Play Mode first.");
  59. return;
  60. }
  61. SetupMapScene();
  62. }
  63. /// <summary>
  64. /// Sets up the team overview controller
  65. /// </summary>
  66. private void SetupTeamOverview()
  67. {
  68. // Look for UI GameObject
  69. GameObject uiGameObject = GameObject.Find("UI");
  70. if (uiGameObject == null)
  71. {
  72. if (showDebugLogs)
  73. {
  74. Debug.LogWarning("⚠️ No UI GameObject found - Team Overview not setup");
  75. }
  76. return;
  77. }
  78. // Check if TeamOverviewController already exists
  79. var existingController = FindFirstObjectByType<TeamOverviewController>();
  80. if (existingController != null)
  81. {
  82. if (showDebugLogs)
  83. {
  84. Debug.Log("📋 TeamOverviewController already exists");
  85. }
  86. return;
  87. }
  88. // Create a new GameObject for the TeamOverviewController
  89. GameObject teamOverviewObject = new GameObject("TeamOverviewController");
  90. teamOverviewObject.transform.SetParent(uiGameObject.transform);
  91. // Add the TeamOverviewController component
  92. var teamOverviewController = teamOverviewObject.AddComponent<TeamOverviewController>();
  93. if (showDebugLogs)
  94. {
  95. Debug.Log("✅ TeamOverviewController created and setup");
  96. }
  97. }
  98. /// <summary>
  99. /// Sets up the travel system (delegates to existing setup if available)
  100. /// </summary>
  101. private void SetupTravelSystem()
  102. {
  103. var travelSystemSetup = FindFirstObjectByType<TravelSystemSetup>();
  104. if (travelSystemSetup != null)
  105. {
  106. if (showDebugLogs)
  107. {
  108. Debug.Log("🚗 Using existing TravelSystemSetup");
  109. }
  110. // Let the existing travel system setup handle this
  111. return;
  112. }
  113. // Look for UI GameObject
  114. GameObject uiGameObject = GameObject.Find("UI");
  115. if (uiGameObject == null)
  116. {
  117. if (showDebugLogs)
  118. {
  119. Debug.LogWarning("⚠️ No UI GameObject found - Travel System not setup");
  120. }
  121. return;
  122. }
  123. // Check if TravelUIController already exists
  124. var existingUIController = uiGameObject.GetComponent<TravelUIController>();
  125. if (existingUIController != null)
  126. {
  127. if (showDebugLogs)
  128. {
  129. Debug.Log("🚗 TravelUIController already exists");
  130. }
  131. return;
  132. }
  133. // Add TravelUIController to UI GameObject
  134. var uiController = uiGameObject.AddComponent<TravelUIController>();
  135. uiController.showDebugLogs = showDebugLogs;
  136. if (showDebugLogs)
  137. {
  138. Debug.Log("✅ TravelUIController created and setup");
  139. }
  140. }
  141. /// <summary>
  142. /// Sets up the quest system and ensures quest data is loaded
  143. /// </summary>
  144. private void SetupQuestSystem()
  145. {
  146. if (showDebugLogs)
  147. {
  148. Debug.Log("🗺️ Setting up Quest System for Map Scene...");
  149. }
  150. // Ensure QuestManager exists and loads quest data
  151. if (QuestManager.Instance != null)
  152. {
  153. QuestManager.Instance.LoadQuestData();
  154. if (showDebugLogs)
  155. {
  156. var activeQuests = QuestManager.Instance.GetActiveQuests();
  157. Debug.Log($"✅ Quest system ready - {activeQuests.Count} active quest(s)");
  158. }
  159. }
  160. else
  161. {
  162. if (showDebugLogs)
  163. {
  164. Debug.LogWarning("⚠️ QuestManager not found in scene");
  165. }
  166. }
  167. // Ensure quest markers are refreshed
  168. var questMapMarkerManager = FindFirstObjectByType<QuestMapMarkerManager>();
  169. if (questMapMarkerManager != null)
  170. {
  171. questMapMarkerManager.RefreshAllMarkers();
  172. if (showDebugLogs)
  173. {
  174. Debug.Log("✅ Quest markers refreshed");
  175. }
  176. }
  177. else
  178. {
  179. if (showDebugLogs)
  180. {
  181. Debug.LogWarning("⚠️ QuestMapMarkerManager not found in scene");
  182. }
  183. }
  184. // Ensure ActiveQuestUI is refreshed
  185. var activeQuestUI = FindFirstObjectByType<ActiveQuestUI>();
  186. if (activeQuestUI != null)
  187. {
  188. // The ActiveQuestUI should automatically refresh via events
  189. if (showDebugLogs)
  190. {
  191. Debug.Log("✅ ActiveQuestUI found and will auto-refresh");
  192. }
  193. }
  194. else
  195. {
  196. // Create ActiveQuestUI if it doesn't exist
  197. SetupActiveQuestUI();
  198. }
  199. }
  200. /// <summary>
  201. /// Sets up the ActiveQuestUI component
  202. /// </summary>
  203. private void SetupActiveQuestUI()
  204. {
  205. if (showDebugLogs)
  206. {
  207. Debug.Log("🎯 Setting up ActiveQuestUI...");
  208. }
  209. // Look for or create a QuestUI GameObject
  210. GameObject questUIGameObject = GameObject.Find("QuestUI");
  211. if (questUIGameObject == null)
  212. {
  213. questUIGameObject = new GameObject("QuestUI");
  214. }
  215. // Check if ActiveQuestUI already exists
  216. var existingActiveQuestUI = questUIGameObject.GetComponent<ActiveQuestUI>();
  217. if (existingActiveQuestUI != null)
  218. {
  219. if (showDebugLogs)
  220. {
  221. Debug.Log("✅ ActiveQuestUI component already exists");
  222. }
  223. return;
  224. }
  225. // Add UIDocument if it doesn't exist
  226. var uiDocument = questUIGameObject.GetComponent<UIDocument>();
  227. if (uiDocument == null)
  228. {
  229. uiDocument = questUIGameObject.AddComponent<UIDocument>();
  230. }
  231. // Load the UXML asset
  232. var activeQuestUIAsset = Resources.Load<VisualTreeAsset>("UI/MapScene/ActiveQuestUI");
  233. if (activeQuestUIAsset != null)
  234. {
  235. uiDocument.visualTreeAsset = activeQuestUIAsset;
  236. if (showDebugLogs)
  237. {
  238. Debug.Log("✅ Loaded ActiveQuestUI.uxml");
  239. }
  240. }
  241. else
  242. {
  243. Debug.LogError("❌ Could not load ActiveQuestUI.uxml from Resources/UI/MapScene/");
  244. return;
  245. }
  246. // Load the USS asset
  247. var activeQuestUIStyleSheet = Resources.Load<StyleSheet>("UI/MapScene/ActiveQuestUI");
  248. if (activeQuestUIStyleSheet != null)
  249. {
  250. uiDocument.rootVisualElement.styleSheets.Add(activeQuestUIStyleSheet);
  251. if (showDebugLogs)
  252. {
  253. Debug.Log("✅ Loaded ActiveQuestUI.uss");
  254. }
  255. }
  256. else
  257. {
  258. Debug.LogWarning("⚠️ Could not load ActiveQuestUI.uss from Resources/UI/MapScene/");
  259. }
  260. // Add the ActiveQuestUI component
  261. var activeQuestUI = questUIGameObject.AddComponent<ActiveQuestUI>();
  262. activeQuestUI.uiDocument = uiDocument;
  263. if (showDebugLogs)
  264. {
  265. Debug.Log("✅ ActiveQuestUI component created and configured");
  266. }
  267. }
  268. /// <summary>
  269. /// Context menu methods for debugging
  270. /// </summary>
  271. [ContextMenu("Setup Team Overview Only")]
  272. public void SetupTeamOverviewOnly()
  273. {
  274. SetupTeamOverview();
  275. }
  276. [ContextMenu("Setup Travel System Only")]
  277. public void SetupTravelSystemOnly()
  278. {
  279. SetupTravelSystem();
  280. }
  281. [ContextMenu("Setup Quest System Only")]
  282. public void SetupQuestSystemOnly()
  283. {
  284. SetupQuestSystem();
  285. }
  286. [ContextMenu("Setup ActiveQuestUI Only")]
  287. public void SetupActiveQuestUIOnly()
  288. {
  289. SetupActiveQuestUI();
  290. }
  291. [ContextMenu("Verify UI Setup")]
  292. public void VerifyUISetup()
  293. {
  294. var uiGameObject = GameObject.Find("UI");
  295. var questUIGameObject = GameObject.Find("QuestUI");
  296. if (uiGameObject == null)
  297. {
  298. Debug.LogError("❌ UI GameObject not found!");
  299. return;
  300. }
  301. var uiDocument = uiGameObject.GetComponent<UIDocument>();
  302. var teamOverviewController = FindFirstObjectByType<TeamOverviewController>();
  303. var travelUIController = uiGameObject.GetComponent<TravelUIController>();
  304. var activeQuestUI = FindFirstObjectByType<ActiveQuestUI>();
  305. Debug.Log($"🔍 UI Setup Verification:");
  306. Debug.Log($" UIDocument: {(uiDocument != null ? "✅" : "❌")}");
  307. Debug.Log($" TeamOverviewController: {(teamOverviewController != null ? "✅" : "❌")}");
  308. Debug.Log($" TravelUIController: {(travelUIController != null ? "✅" : "❌")}");
  309. Debug.Log($" ActiveQuestUI: {(activeQuestUI != null ? "✅" : "❌")}");
  310. Debug.Log($" QuestUI GameObject: {(questUIGameObject != null ? "✅" : "❌")}");
  311. if (uiDocument?.rootVisualElement != null)
  312. {
  313. var root = uiDocument.rootVisualElement;
  314. var teamOverviewPanel = root.Q("TeamOverviewPanel");
  315. var travelContainer = root.Q("TravelContainer");
  316. Debug.Log($" TeamOverviewPanel in UXML: {(teamOverviewPanel != null ? "✅" : "❌")}");
  317. Debug.Log($" TravelContainer in UXML: {(travelContainer != null ? "✅" : "❌")}");
  318. }
  319. if (activeQuestUI?.uiDocument?.rootVisualElement != null)
  320. {
  321. var questRoot = activeQuestUI.uiDocument.rootVisualElement;
  322. var questTracker = questRoot.Q("quest-tracker");
  323. Debug.Log($" Quest Tracker in UXML: {(questTracker != null ? "✅" : "❌")}");
  324. }
  325. }
  326. }