| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- 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()
- {
- Debug.Log("SetupUI called");
- // Get the UI Document component
- if (uiDocument == null)
- uiDocument = GetComponent<UIDocument>();
- if (uiDocument == null)
- {
- Debug.LogError("UIDocument component not found on MapLegendUI GameObject!");
- return;
- }
- Debug.Log("UIDocument found");
- // Load the UXML template if not assigned
- if (legendTemplate == null)
- {
- // Try multiple paths to find the UXML file
- legendTemplate = Resources.Load<VisualTreeAsset>("UI/MapLegend");
- if (legendTemplate == null)
- {
- legendTemplate = Resources.Load<VisualTreeAsset>("MapLegend");
- if (legendTemplate == null)
- {
- Debug.LogError("MapLegend.uxml not found! Make sure it's in a Resources folder.");
- return;
- }
- }
- }
- Debug.Log("UXML template loaded");
- // Load the USS stylesheet if not assigned
- if (legendStyleSheet == null)
- {
- // Try multiple paths to find the USS file
- legendStyleSheet = Resources.Load<StyleSheet>("UI/MapLegend");
- if (legendStyleSheet == null)
- {
- legendStyleSheet = Resources.Load<StyleSheet>("MapLegend");
- if (legendStyleSheet == null)
- {
- Debug.LogWarning("MapLegend.uss not found - using default styling");
- }
- }
- }
- Debug.Log("USS stylesheet loaded");
- // Clone the template and add it to the document
- root = uiDocument.rootVisualElement;
- var legendElement = legendTemplate.Instantiate();
- Debug.Log("Template instantiated");
- // 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;
- Debug.Log("Legend initialized as visible");
- }
- Debug.Log("Legend UI setup complete");
- }
- }
|