using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements; using System.Linq; public class ItemShopManager : MonoBehaviour { [Header("Shop Settings")] public string shopName = "General Store"; [Header("Shop Items (ScriptableObjects)")] [SerializeField] private List availableItems = new List(); private UIDocument uiDocument; private TeamCharacter currentCustomer; // Callback for when character data changes public System.Action OnCharacterDataChanged; // UI Elements private VisualElement shopContainer; private TextField searchField; private DropdownField categoryFilter; private ScrollView itemList; private Label shopTitle; private Label playerMoney; private Button closeButton; // Current filter state private string currentCategory = "All"; private string currentSearchTerm = ""; void Awake() { // Load all Item ScriptableObjects from Resources LoadAllItems(); } void Start() { // Initialize UI in Start() to ensure UIDocument is ready uiDocument = GetComponent(); if (uiDocument == null) { Debug.LogError("ItemShopManager requires a UIDocument component"); return; } if (uiDocument.visualTreeAsset == null) { Debug.LogError("ItemShopManager: UIDocument needs a Visual Tree Asset assigned! Please assign ShopUI.uxml"); return; } // Ensure panel settings and proper sorting order are set EnsurePanelSettings(); InitializeUI(); } void LoadAllItems() { availableItems.Clear(); // Load all Item ScriptableObjects from Resources/Items folder and subfolders Item[] allItems = Resources.LoadAll("Items"); if (allItems.Length == 0) { Debug.LogWarning("No Item ScriptableObjects found in Resources/Items folder. Creating some default items..."); CreateDefaultItems(); } else { availableItems.AddRange(allItems); Debug.Log($"Loaded {allItems.Length} items from Resources/Items folder"); } // Also check for items in the direct Resources folder Item[] directItems = Resources.LoadAll(""); foreach (Item item in directItems) { if (!availableItems.Contains(item)) { availableItems.Add(item); } } Debug.Log($"Total items available in shop: {availableItems.Count}"); foreach (Item item in availableItems) { Debug.Log($"- {item.itemName} ({item.itemType})"); } } void CreateDefaultItems() { // Create some default items if none are found // These will be temporary runtime items, but you should create proper ScriptableObject assets // Note: This creates runtime instances, not asset files var simpleSword = ScriptableObject.CreateInstance(); simpleSword.itemName = "Basic Sword"; // Match actual asset name simpleSword.description = "A basic sword for beginners."; simpleSword.itemType = ItemType.Weapon; simpleSword.goldCost = 10; simpleSword.minDamage = 1; simpleSword.maxDamage = 6; simpleSword.weaponType = WeaponType.Sword; simpleSword.searchTags = new string[] { "sword", "melee", "blade", "basic" }; availableItems.Add(simpleSword); var simpleBow = ScriptableObject.CreateInstance(); simpleBow.itemName = "Basic Bow"; // Match actual asset name simpleBow.description = "A basic bow for shooting arrows."; simpleBow.itemType = ItemType.Weapon; simpleBow.goldCost = 15; simpleBow.minDamage = 1; simpleBow.maxDamage = 4; simpleBow.range = 150; simpleBow.weaponType = WeaponType.Bow; simpleBow.searchTags = new string[] { "bow", "ranged", "arrow", "basic" }; availableItems.Add(simpleBow); var leatherArmor = ScriptableObject.CreateInstance(); leatherArmor.itemName = "Leather Armor"; leatherArmor.description = "Basic leather protection."; leatherArmor.itemType = ItemType.Armor; leatherArmor.goldCost = 12; leatherArmor.armorClass = 1; leatherArmor.armorType = ArmorType.Light; leatherArmor.armorSlot = ArmorSlot.Chest; leatherArmor.searchTags = new string[] { "leather", "armor", "chest", "light" }; availableItems.Add(leatherArmor); var healthPotion = ScriptableObject.CreateInstance(); healthPotion.itemName = "Health Potion"; healthPotion.description = "Restores health when consumed."; healthPotion.itemType = ItemType.Consumable; healthPotion.goldCost = 3; healthPotion.isConsumable = true; healthPotion.healthDiceCount = 1; healthPotion.healthDiceType = 6; healthPotion.healthBonus = 1; healthPotion.searchTags = new string[] { "potion", "health", "healing", "consumable" }; availableItems.Add(healthPotion); Debug.Log("Created default runtime items since no ScriptableObject assets were found."); } void InitializeUI() { var root = uiDocument.rootVisualElement; Debug.Log("ItemShopManager: Initializing UI elements..."); shopContainer = root.Q("ShopContainer"); searchField = root.Q("SearchField"); categoryFilter = root.Q("CategoryFilter"); itemList = root.Q("ItemList"); shopTitle = root.Q