TeamOverviewSetup.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. /// <summary>
  4. /// Setup script to create and configure the TeamOverview UI system.
  5. /// This creates a separate UI GameObject specifically for the team overview panel.
  6. /// </summary>
  7. public class TeamOverviewSetup : MonoBehaviour
  8. {
  9. [Header("Setup Options")]
  10. public bool autoSetupOnStart = true;
  11. public bool showDebugLogs = true;
  12. [Header("UI Assets")]
  13. [SerializeField] private VisualTreeAsset teamOverviewUXML;
  14. [SerializeField] private StyleSheet teamOverviewStyleSheet;
  15. void Start()
  16. {
  17. if (autoSetupOnStart)
  18. {
  19. SetupTeamOverview();
  20. }
  21. }
  22. [ContextMenu("Setup Team Overview UI")]
  23. public void SetupTeamOverview()
  24. {
  25. if (!Application.isPlaying)
  26. {
  27. Debug.LogWarning("⚠️ TeamOverviewSetup: Please run this setup during Play Mode!");
  28. return;
  29. }
  30. // Check if TeamOverview already exists
  31. var existingOverview = FindFirstObjectByType<TeamOverviewController>();
  32. if (existingOverview != null)
  33. {
  34. if (showDebugLogs)
  35. {
  36. Debug.Log("📋 TeamOverview already exists, skipping setup");
  37. }
  38. return;
  39. }
  40. // Try to load UXML asset if not assigned
  41. if (teamOverviewUXML == null)
  42. {
  43. teamOverviewUXML = Resources.Load<VisualTreeAsset>("UI/MapScene/TeamOverview");
  44. if (teamOverviewUXML == null)
  45. {
  46. #if UNITY_EDITOR
  47. // Try alternative paths (Editor only)
  48. teamOverviewUXML = UnityEditor.AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/UI/MapScene/TeamOverview.uxml");
  49. #endif
  50. }
  51. }
  52. if (teamOverviewUXML == null)
  53. {
  54. Debug.LogError("❌ TeamOverview.uxml not found! Please assign it in the inspector.");
  55. return;
  56. }
  57. // Create TeamOverview GameObject
  58. GameObject teamOverviewObject = new GameObject("TeamOverviewUI");
  59. teamOverviewObject.transform.SetParent(transform);
  60. // Add UIDocument component
  61. UIDocument uiDocument = teamOverviewObject.AddComponent<UIDocument>();
  62. uiDocument.visualTreeAsset = teamOverviewUXML;
  63. // Find and assign Panel Settings from existing UI
  64. var existingUI = GameObject.Find("TravelUI")?.GetComponent<UIDocument>();
  65. if (existingUI?.panelSettings != null)
  66. {
  67. uiDocument.panelSettings = existingUI.panelSettings;
  68. if (showDebugLogs)
  69. {
  70. Debug.Log("✅ Panel Settings assigned from TravelUI");
  71. }
  72. }
  73. else
  74. {
  75. // Try to find any PanelSettings asset
  76. #if UNITY_EDITOR
  77. var panelSettings = UnityEditor.AssetDatabase.FindAssets("t:PanelSettings");
  78. if (panelSettings.Length > 0)
  79. {
  80. var path = UnityEditor.AssetDatabase.GUIDToAssetPath(panelSettings[0]);
  81. var settings = UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEngine.UIElements.PanelSettings>(path);
  82. uiDocument.panelSettings = settings;
  83. if (showDebugLogs)
  84. {
  85. Debug.Log($"✅ Panel Settings auto-assigned: {settings.name}");
  86. }
  87. }
  88. #endif
  89. }
  90. // Add style sheet if available (the UXML file should reference it automatically)
  91. if (teamOverviewStyleSheet != null && showDebugLogs)
  92. {
  93. Debug.Log("ℹ️ Style sheet will be loaded automatically from UXML reference");
  94. }
  95. // Set up the UI to appear on the right side with appropriate priority
  96. uiDocument.sortingOrder = 5; // Lower than combat popups but higher than map UI
  97. // Add TeamOverviewController
  98. TeamOverviewController controller = teamOverviewObject.AddComponent<TeamOverviewController>();
  99. controller.showDebugLogs = showDebugLogs;
  100. // Position the UI panel on the right side
  101. StartCoroutine(PositionTeamOverview(uiDocument));
  102. // Ensure TravelEventUI exists for travel events
  103. EnsureTravelEventUI();
  104. // Ensure player object exists for MapMaker2
  105. EnsurePlayerObject();
  106. // Ensure combat popups have highest priority
  107. EnsureCombatPopupPriority();
  108. if (showDebugLogs)
  109. {
  110. Debug.Log("✅ TeamOverview UI created and configured!");
  111. }
  112. }
  113. private System.Collections.IEnumerator PositionTeamOverview(UIDocument uiDocument)
  114. {
  115. // Wait a frame for the UI to initialize
  116. yield return null;
  117. var root = uiDocument.rootVisualElement;
  118. var teamPanel = root.Q("TeamOverviewPanel");
  119. if (teamPanel != null)
  120. {
  121. // Position the panel on the right side of the screen
  122. teamPanel.style.position = Position.Absolute;
  123. teamPanel.style.right = 10;
  124. teamPanel.style.top = 10;
  125. teamPanel.style.width = 300;
  126. teamPanel.style.height = Length.Percent(80);
  127. // Make sure it's visible
  128. teamPanel.style.display = DisplayStyle.Flex;
  129. if (showDebugLogs)
  130. {
  131. Debug.Log("📍 TeamOverview positioned on right side");
  132. }
  133. }
  134. }
  135. /// <summary>
  136. /// Ensures TravelEventUI exists in the scene to prevent warnings
  137. /// </summary>
  138. private void EnsureTravelEventUI()
  139. {
  140. var existingEventUI = FindFirstObjectByType<TravelEventUI>();
  141. if (existingEventUI == null)
  142. {
  143. // Create a simple TravelEventUI GameObject
  144. GameObject eventUIObject = new GameObject("TravelEventUI");
  145. eventUIObject.transform.SetParent(transform);
  146. var travelEventUI = eventUIObject.AddComponent<TravelEventUI>();
  147. if (showDebugLogs)
  148. {
  149. Debug.Log("✅ TravelEventUI created to handle travel event messages");
  150. }
  151. }
  152. else if (showDebugLogs)
  153. {
  154. Debug.Log("ℹ️ TravelEventUI already exists");
  155. }
  156. }
  157. /// <summary>
  158. /// Ensures a basic player object exists for MapMaker2
  159. /// </summary>
  160. private void EnsurePlayerObject()
  161. {
  162. // Check if a player object exists
  163. var player = GameObject.FindWithTag("Player");
  164. if (player == null)
  165. {
  166. // Try to find any object that might be the player/team
  167. var teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  168. if (teamPlacement != null)
  169. {
  170. // Tag the team placement as Player if it exists
  171. teamPlacement.gameObject.tag = "Player";
  172. if (showDebugLogs)
  173. {
  174. Debug.Log("✅ Tagged SimpleTeamPlacement as Player for MapMaker2");
  175. }
  176. }
  177. else
  178. {
  179. // Create a simple player marker
  180. GameObject playerMarker = new GameObject("PlayerMarker");
  181. playerMarker.tag = "Player";
  182. if (showDebugLogs)
  183. {
  184. Debug.Log("✅ Created basic Player object for MapMaker2");
  185. }
  186. }
  187. }
  188. else if (showDebugLogs)
  189. {
  190. Debug.Log("ℹ️ Player object already exists");
  191. }
  192. }
  193. /// <summary>
  194. /// Ensures combat popups have the highest UI priority to prevent black squares
  195. /// </summary>
  196. private void EnsureCombatPopupPriority()
  197. {
  198. // Find combat popup UI documents and ensure they have highest sorting order
  199. var combatPopup = FindFirstObjectByType<CombatEventPopup>();
  200. if (combatPopup != null)
  201. {
  202. var popupUIDocument = combatPopup.GetComponent<UIDocument>();
  203. if (popupUIDocument != null)
  204. {
  205. popupUIDocument.sortingOrder = 100; // Highest priority
  206. if (showDebugLogs)
  207. {
  208. Debug.Log("✅ CombatEventPopup sortingOrder set to 100 (highest priority)");
  209. }
  210. }
  211. }
  212. var combatPopupUXML = FindFirstObjectByType<CombatEventPopupUXML>();
  213. if (combatPopupUXML != null)
  214. {
  215. var popupUIDocument = combatPopupUXML.GetComponent<UIDocument>();
  216. if (popupUIDocument != null)
  217. {
  218. popupUIDocument.sortingOrder = 100; // Highest priority
  219. if (showDebugLogs)
  220. {
  221. Debug.Log("✅ CombatEventPopupUXML sortingOrder set to 100 (highest priority)");
  222. }
  223. }
  224. }
  225. if (showDebugLogs && combatPopup == null && combatPopupUXML == null)
  226. {
  227. Debug.Log("ℹ️ No combat popup components found - will be configured when created");
  228. }
  229. }
  230. [ContextMenu("Remove Team Overview")]
  231. public void RemoveTeamOverview()
  232. {
  233. var existingOverview = FindFirstObjectByType<TeamOverviewController>();
  234. if (existingOverview != null)
  235. {
  236. DestroyImmediate(existingOverview.gameObject);
  237. Debug.Log("🗑️ TeamOverview removed");
  238. }
  239. else
  240. {
  241. Debug.Log("ℹ️ No TeamOverview found to remove");
  242. }
  243. }
  244. [ContextMenu("Test Team Overview")]
  245. public void TestTeamOverview()
  246. {
  247. var controller = FindFirstObjectByType<TeamOverviewController>();
  248. if (controller != null)
  249. {
  250. controller.TestShowTeamOverview();
  251. }
  252. else
  253. {
  254. Debug.LogWarning("⚠️ No TeamOverviewController found! Run setup first.");
  255. }
  256. }
  257. [ContextMenu("Fix UI Layering Issues")]
  258. public void FixUILayering()
  259. {
  260. Debug.Log("🔧 Fixing UI layering issues...");
  261. // Set correct sorting orders for all UI systems
  262. var teamOverview = FindFirstObjectByType<TeamOverviewController>()?.GetComponent<UIDocument>();
  263. if (teamOverview != null)
  264. {
  265. teamOverview.sortingOrder = 5;
  266. Debug.Log("✅ TeamOverview sortingOrder: 5");
  267. }
  268. var travelUI = GameObject.Find("TravelUI")?.GetComponent<UIDocument>();
  269. if (travelUI != null)
  270. {
  271. travelUI.sortingOrder = 3;
  272. Debug.Log("✅ TravelUI sortingOrder: 3");
  273. }
  274. var combatPopup = FindFirstObjectByType<CombatEventPopup>()?.GetComponent<UIDocument>();
  275. if (combatPopup != null)
  276. {
  277. combatPopup.sortingOrder = 100;
  278. Debug.Log("✅ CombatEventPopup sortingOrder: 100");
  279. }
  280. var combatPopupUXML = FindFirstObjectByType<CombatEventPopupUXML>()?.GetComponent<UIDocument>();
  281. if (combatPopupUXML != null)
  282. {
  283. combatPopupUXML.sortingOrder = 100;
  284. Debug.Log("✅ CombatEventPopupUXML sortingOrder: 100");
  285. }
  286. Debug.Log("🎯 UI layering fixed! Combat popups should now appear on top.");
  287. }
  288. }