MapUISetupHelper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. /// <summary>
  4. /// Helper script to automatically assign the correct UXML file to the UI GameObject
  5. /// and verify the travel UI setup.
  6. /// </summary>
  7. [System.Serializable]
  8. public class MapUISetupHelper : MonoBehaviour
  9. {
  10. [Header("UI Setup")]
  11. [SerializeField] private VisualTreeAsset mapWithTravelUITemplate;
  12. [Header("Debug Info")]
  13. [SerializeField] private bool showDebugInfo = false;
  14. void Start()
  15. {
  16. SetupUI();
  17. }
  18. [ContextMenu("Setup UI")]
  19. public void SetupUI()
  20. {
  21. // Find the UI GameObject
  22. GameObject uiGameObject = GameObject.Find("UI");
  23. if (uiGameObject == null)
  24. {
  25. Debug.LogError("UI GameObject not found!");
  26. return;
  27. }
  28. // Get the UIDocument component
  29. UIDocument uiDocument = uiGameObject.GetComponent<UIDocument>();
  30. if (uiDocument == null)
  31. {
  32. Debug.LogError("UIDocument component not found on UI GameObject!");
  33. return;
  34. }
  35. // Try to load the UXML file if not assigned in inspector
  36. if (mapWithTravelUITemplate == null)
  37. {
  38. mapWithTravelUITemplate = Resources.Load<VisualTreeAsset>("UI/Map/MapWithTravelUI");
  39. if (mapWithTravelUITemplate == null)
  40. {
  41. // Try direct path
  42. mapWithTravelUITemplate = UnityEditor.AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/UI/Map/MapWithTravelUI.uxml");
  43. }
  44. }
  45. // Assign the UXML template
  46. if (mapWithTravelUITemplate != null)
  47. {
  48. uiDocument.visualTreeAsset = mapWithTravelUITemplate;
  49. Debug.Log("Successfully assigned MapWithTravelUI.uxml to UI GameObject!");
  50. if (showDebugInfo)
  51. {
  52. VerifyUIElements();
  53. }
  54. }
  55. else
  56. {
  57. Debug.LogError("Could not find MapWithTravelUI.uxml! Please assign it manually in the inspector.");
  58. }
  59. }
  60. [ContextMenu("Verify UI Elements")]
  61. public void VerifyUIElements()
  62. {
  63. GameObject uiGameObject = GameObject.Find("UI");
  64. if (uiGameObject == null) return;
  65. UIDocument uiDocument = uiGameObject.GetComponent<UIDocument>();
  66. if (uiDocument == null || uiDocument.rootVisualElement == null) return;
  67. var root = uiDocument.rootVisualElement;
  68. Debug.Log("=== UI ELEMENT VERIFICATION ===");
  69. Debug.Log($"Root element exists: {root != null}");
  70. // Check MapLegend
  71. var mapLegend = root.Q("MapLegend");
  72. Debug.Log($"MapLegend found: {mapLegend != null}");
  73. // Check TravelContainer and its children
  74. var travelContainer = root.Q("TravelContainer");
  75. Debug.Log($"TravelContainer found: {travelContainer != null}");
  76. if (travelContainer != null)
  77. {
  78. var travelPanel = root.Q("TravelPanel");
  79. var travelHeader = root.Q("TravelHeader");
  80. var closeButton = root.Q<Button>("CloseButton");
  81. var distanceLabel = root.Q<Label>("DistanceLabel");
  82. var timeLabel = root.Q<Label>("TimeLabel");
  83. var specialCostsLabel = root.Q<Label>("SpecialCostsLabel");
  84. var startTravelButton = root.Q<Button>("StartTravelButton");
  85. var cancelTravelButton = root.Q<Button>("CancelTravelButton");
  86. var avoidTunnelsToggle = root.Q<Toggle>("AvoidTunnelsToggle");
  87. var avoidFerriesToggle = root.Q<Toggle>("AvoidFerriesToggle");
  88. var alternativeRouteButton = root.Q<Button>("AlternativeRouteButton");
  89. Debug.Log($"TravelPanel: {travelPanel != null}");
  90. Debug.Log($"TravelHeader: {travelHeader != null}");
  91. Debug.Log($"CloseButton: {closeButton != null}");
  92. Debug.Log($"DistanceLabel: {distanceLabel != null}");
  93. Debug.Log($"TimeLabel: {timeLabel != null}");
  94. Debug.Log($"SpecialCostsLabel: {specialCostsLabel != null}");
  95. Debug.Log($"StartTravelButton: {startTravelButton != null}");
  96. Debug.Log($"CancelTravelButton: {cancelTravelButton != null}");
  97. Debug.Log($"AvoidTunnelsToggle: {avoidTunnelsToggle != null}");
  98. Debug.Log($"AvoidFerriesToggle: {avoidFerriesToggle != null}");
  99. Debug.Log($"AlternativeRouteButton: {alternativeRouteButton != null}");
  100. // Check if TravelContainer is hidden (which is correct)
  101. var displayStyle = travelContainer.style.display;
  102. Debug.Log($"TravelContainer display style: {displayStyle.value}");
  103. Debug.Log($"TravelContainer should be hidden by default: {displayStyle.value == DisplayStyle.None}");
  104. }
  105. Debug.Log("=== END UI ELEMENT VERIFICATION ===");
  106. }
  107. [ContextMenu("Show Travel UI (Test)")]
  108. public void ShowTravelUITest()
  109. {
  110. GameObject uiGameObject = GameObject.Find("UI");
  111. if (uiGameObject == null) return;
  112. UIDocument uiDocument = uiGameObject.GetComponent<UIDocument>();
  113. if (uiDocument == null || uiDocument.rootVisualElement == null) return;
  114. var travelContainer = uiDocument.rootVisualElement.Q("TravelContainer");
  115. if (travelContainer != null)
  116. {
  117. travelContainer.style.display = DisplayStyle.Flex;
  118. Debug.Log("Travel UI forced to show for testing");
  119. // Set some test data
  120. var distanceLabel = uiDocument.rootVisualElement.Q<Label>("DistanceLabel");
  121. var timeLabel = uiDocument.rootVisualElement.Q<Label>("TimeLabel");
  122. var specialCostsLabel = uiDocument.rootVisualElement.Q<Label>("SpecialCostsLabel");
  123. var startButton = uiDocument.rootVisualElement.Q<Button>("StartTravelButton");
  124. if (distanceLabel != null) distanceLabel.text = "Distance: 12.5 leagues";
  125. if (timeLabel != null) timeLabel.text = "Travel Time: 8.3 hours";
  126. if (specialCostsLabel != null) specialCostsLabel.text = "Special Costs: 15 gold for tunnel passage";
  127. if (startButton != null) startButton.text = "Start Journey (15 gold)";
  128. }
  129. else
  130. {
  131. Debug.LogError("TravelContainer not found!");
  132. }
  133. }
  134. [ContextMenu("Hide Travel UI")]
  135. public void HideTravelUITest()
  136. {
  137. GameObject uiGameObject = GameObject.Find("UI");
  138. if (uiGameObject == null) return;
  139. UIDocument uiDocument = uiGameObject.GetComponent<UIDocument>();
  140. if (uiDocument == null || uiDocument.rootVisualElement == null) return;
  141. var travelContainer = uiDocument.rootVisualElement.Q("TravelContainer");
  142. if (travelContainer != null)
  143. {
  144. travelContainer.style.display = DisplayStyle.None;
  145. Debug.Log("Travel UI hidden");
  146. }
  147. }
  148. }