using UnityEngine; using UnityEngine.UIElements; public class MapLegendUI : MonoBehaviour { [Header("UI References")] public UIDocument uiDocument; public VisualTreeAsset legendTemplate; public StyleSheet legendStyleSheet; private VisualElement root; void Start() { SetupUI(); } void SetupUI() { // Get the UI Document component if (uiDocument == null) uiDocument = GetComponent(); if (uiDocument == null) { Debug.LogError("UIDocument component not found on MapLegendUI GameObject!"); return; } // Load the UXML template if not assigned if (legendTemplate == null) { // Try multiple paths to find the UXML file legendTemplate = Resources.Load("UI/MapLegend"); if (legendTemplate == null) { legendTemplate = Resources.Load("MapLegend"); if (legendTemplate == null) { Debug.LogError("MapLegend.uxml not found! Make sure it's in a Resources folder."); return; } } } // Load the USS stylesheet if not assigned if (legendStyleSheet == null) { // Try multiple paths to find the USS file legendStyleSheet = Resources.Load("UI/MapLegend"); if (legendStyleSheet == null) { legendStyleSheet = Resources.Load("MapLegend"); if (legendStyleSheet == null) { Debug.LogWarning("MapLegend.uss not found - using default styling"); } } } // Clone the template and add it to the document root = uiDocument.rootVisualElement; var legendElement = legendTemplate.Instantiate(); // Add the stylesheet if available if (legendStyleSheet != null) { root.styleSheets.Add(legendStyleSheet); } // Add the legend to the root root.Add(legendElement); // Initialize legend as visible var legend = root.Q("MapLegend"); if (legend != null) { legend.style.display = DisplayStyle.Flex; } } }