using UnityEngine;
using UnityEngine.UIElements;
///
/// Helper script to automatically assign the correct UXML file to the UI GameObject
/// and verify the travel UI setup.
///
[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();
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("UI/Map/MapWithTravelUI");
if (mapWithTravelUITemplate == null)
{
// Try direct path
mapWithTravelUITemplate = UnityEditor.AssetDatabase.LoadAssetAtPath("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();
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