| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- 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<UIDocument>();
- 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<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;
- }
- }
- }
- // 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");
- }
- }
- }
- // 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;
- }
- }
- }
|