| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- using UnityEngine;
- using UnityEngine.UIElements;
- /// <summary>
- /// Helper script to automatically assign the correct UXML file to the UI GameObject
- /// and verify the travel UI setup.
- /// </summary>
- [System.Serializable]
- public class MapUISetupHelper : MonoBehaviour
- {
- [Header("UI Setup")]
- [SerializeField] private VisualTreeAsset mapWithTravelUITemplate;
- [Header("Debug Info")]
- [SerializeField] private bool showDebugInfo = false;
- void Start()
- {
- SetupUI();
- }
- [ContextMenu("Setup UI")]
- public void SetupUI()
- {
- // Find the UI GameObject
- GameObject uiGameObject = GameObject.Find("UI");
- if (uiGameObject == null)
- {
- Debug.LogError("UI GameObject not found!");
- return;
- }
- // Get the UIDocument component
- UIDocument uiDocument = uiGameObject.GetComponent<UIDocument>();
- if (uiDocument == null)
- {
- Debug.LogError("UIDocument component not found on UI GameObject!");
- return;
- }
- // Try to load the UXML file if not assigned in inspector
- if (mapWithTravelUITemplate == null)
- {
- mapWithTravelUITemplate = Resources.Load<VisualTreeAsset>("UI/Map/MapWithTravelUI");
- if (mapWithTravelUITemplate == null)
- {
- // Try direct path
- mapWithTravelUITemplate = UnityEditor.AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/UI/Map/MapWithTravelUI.uxml");
- }
- }
- // Assign the UXML template
- if (mapWithTravelUITemplate != null)
- {
- uiDocument.visualTreeAsset = mapWithTravelUITemplate;
- Debug.Log("Successfully assigned MapWithTravelUI.uxml to UI GameObject!");
- if (showDebugInfo)
- {
- VerifyUIElements();
- }
- }
- else
- {
- Debug.LogError("Could not find MapWithTravelUI.uxml! Please assign it manually in the inspector.");
- }
- }
- [ContextMenu("Verify UI Elements")]
- public void VerifyUIElements()
- {
- GameObject uiGameObject = GameObject.Find("UI");
- if (uiGameObject == null) return;
- UIDocument uiDocument = uiGameObject.GetComponent<UIDocument>();
- if (uiDocument == null || uiDocument.rootVisualElement == null) return;
- var root = uiDocument.rootVisualElement;
- Debug.Log("=== UI ELEMENT VERIFICATION ===");
- Debug.Log($"Root element exists: {root != null}");
- // Check MapLegend
- var mapLegend = root.Q("MapLegend");
- Debug.Log($"MapLegend found: {mapLegend != null}");
- // Check TravelContainer and its children
- var travelContainer = root.Q("TravelContainer");
- Debug.Log($"TravelContainer found: {travelContainer != null}");
- if (travelContainer != null)
- {
- var travelPanel = root.Q("TravelPanel");
- var travelHeader = root.Q("TravelHeader");
- var closeButton = root.Q<Button>("CloseButton");
- var distanceLabel = root.Q<Label>("DistanceLabel");
- var timeLabel = root.Q<Label>("TimeLabel");
- var specialCostsLabel = root.Q<Label>("SpecialCostsLabel");
- var startTravelButton = root.Q<Button>("StartTravelButton");
- var cancelTravelButton = root.Q<Button>("CancelTravelButton");
- var avoidTunnelsToggle = root.Q<Toggle>("AvoidTunnelsToggle");
- var avoidFerriesToggle = root.Q<Toggle>("AvoidFerriesToggle");
- var alternativeRouteButton = root.Q<Button>("AlternativeRouteButton");
- Debug.Log($"TravelPanel: {travelPanel != null}");
- Debug.Log($"TravelHeader: {travelHeader != null}");
- Debug.Log($"CloseButton: {closeButton != null}");
- Debug.Log($"DistanceLabel: {distanceLabel != null}");
- Debug.Log($"TimeLabel: {timeLabel != null}");
- Debug.Log($"SpecialCostsLabel: {specialCostsLabel != null}");
- Debug.Log($"StartTravelButton: {startTravelButton != null}");
- Debug.Log($"CancelTravelButton: {cancelTravelButton != null}");
- Debug.Log($"AvoidTunnelsToggle: {avoidTunnelsToggle != null}");
- Debug.Log($"AvoidFerriesToggle: {avoidFerriesToggle != null}");
- Debug.Log($"AlternativeRouteButton: {alternativeRouteButton != null}");
- // Check if TravelContainer is hidden (which is correct)
- var displayStyle = travelContainer.style.display;
- Debug.Log($"TravelContainer display style: {displayStyle.value}");
- Debug.Log($"TravelContainer should be hidden by default: {displayStyle.value == DisplayStyle.None}");
- }
- Debug.Log("=== END UI ELEMENT VERIFICATION ===");
- }
- [ContextMenu("Show Travel UI (Test)")]
- public void ShowTravelUITest()
- {
- GameObject uiGameObject = GameObject.Find("UI");
- if (uiGameObject == null) return;
- UIDocument uiDocument = uiGameObject.GetComponent<UIDocument>();
- if (uiDocument == null || uiDocument.rootVisualElement == null) return;
- var travelContainer = uiDocument.rootVisualElement.Q("TravelContainer");
- if (travelContainer != null)
- {
- travelContainer.style.display = DisplayStyle.Flex;
- Debug.Log("Travel UI forced to show for testing");
- // Set some test data
- var distanceLabel = uiDocument.rootVisualElement.Q<Label>("DistanceLabel");
- var timeLabel = uiDocument.rootVisualElement.Q<Label>("TimeLabel");
- var specialCostsLabel = uiDocument.rootVisualElement.Q<Label>("SpecialCostsLabel");
- var startButton = uiDocument.rootVisualElement.Q<Button>("StartTravelButton");
- if (distanceLabel != null) distanceLabel.text = "Distance: 12.5 leagues";
- if (timeLabel != null) timeLabel.text = "Travel Time: 8.3 hours";
- if (specialCostsLabel != null) specialCostsLabel.text = "Special Costs: 15 gold for tunnel passage";
- if (startButton != null) startButton.text = "Start Journey (15 gold)";
- }
- else
- {
- Debug.LogError("TravelContainer not found!");
- }
- }
- [ContextMenu("Hide Travel UI")]
- public void HideTravelUITest()
- {
- GameObject uiGameObject = GameObject.Find("UI");
- if (uiGameObject == null) return;
- UIDocument uiDocument = uiGameObject.GetComponent<UIDocument>();
- if (uiDocument == null || uiDocument.rootVisualElement == null) return;
- var travelContainer = uiDocument.rootVisualElement.Q("TravelContainer");
- if (travelContainer != null)
- {
- travelContainer.style.display = DisplayStyle.None;
- Debug.Log("Travel UI hidden");
- }
- }
- }
|