using UnityEngine; using UnityEngine.UIElements; using System.Collections.Generic; using System; /// /// UI Toolkit-based item selection UI for battle /// public class BattleItemSelector : MonoBehaviour { [Header("UI References")] public UIDocument uiDocument; [Header("Settings")] public int maxItemsToShow = 10; private Character currentCharacter; private VisualElement rootElement; private VisualElement container; private VisualElement itemList; private Label titleLabel; private Label characterLabel; private VisualElement noItemsContainer; private Button cancelButton; private Button closeButton; // Events public event Action OnItemSelected; public event Action OnSelectionCancelled; void Awake() { if (uiDocument == null) uiDocument = GetComponent(); if (uiDocument == null) { // Create UIDocument if it doesn't exist uiDocument = gameObject.AddComponent(); } } void Start() { InitializeUI(); SetVisible(false); } void Update() { if (container != null && container.style.display == DisplayStyle.Flex) { if (Input.GetKeyDown(KeyCode.Escape)) { OnSelectionCancelled?.Invoke(); HideSelection(); } } } private void InitializeUI() { // Load UXML and USS var visualTreeAsset = Resources.Load("UI/ItemSelectionUI"); var styleSheet = Resources.Load("UI/ItemSelectionUI"); // Try simple version if main version fails if (visualTreeAsset == null) { Debug.LogWarning("Main UXML failed, trying simple version..."); visualTreeAsset = Resources.Load("UI/ItemSelectionUI_Simple"); } if (visualTreeAsset == null) { Debug.LogError("Could not load any ItemSelectionUI.uxml from Resources/UI/"); CreateFallbackUI(); return; } rootElement = uiDocument.rootVisualElement; rootElement.Clear(); // Clone the visual tree var clonedTree = visualTreeAsset.CloneTree(); rootElement.Add(clonedTree); // Apply styles if (styleSheet != null) { rootElement.styleSheets.Add(styleSheet); } // Get UI elements container = rootElement.Q("ItemSelectionContainer"); itemList = rootElement.Q("ItemList"); titleLabel = rootElement.Q