using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; using TMPro; public class ShopManager : MonoBehaviour { [Header("Shop Settings")] public int startingMoney = 100; public Item[] availableItems; // Assumes Item is a MonoBehaviour attached to a Prefab [Header("UI Elements")] public TMP_Text moneyText; public Button[] buyButtons; [Header("Inventory Settings")] public Transform storeRoomTransform; // Where physical items spawn public Transform inventoryUI; // The Panel with the Grid Layout Group public GameObject inventoryTextTemplate; // Drag a Prefab with TMP_Text here! private int currentMoney; private Dictionary inventory = new Dictionary(); void Start() { currentMoney = startingMoney; // Initialize dictionary once at start PopulateInventoryDictionary(); InitializeShopButtons(); UpdateAllUI(); } void InitializeShopButtons() { for (int i = 0; i < buyButtons.Length; i++) { if (i < availableItems.Length) { int index = i; Item item = availableItems[index]; Button button = buyButtons[index]; // Set Button Text TMP_Text btnText = button.GetComponentInChildren(); if (btnText != null) { btnText.text = $"{item.itemName} - ${item.itemPrice}"; } // Add Click Listener button.onClick.RemoveAllListeners(); button.onClick.AddListener(() => BuyItem(item, button)); } else { buyButtons[i].gameObject.SetActive(false); } } } void BuyItem(Item item, Button button) { if (currentMoney >= item.itemPrice) { // 1. Deduct Money currentMoney -= item.itemPrice; // 2. Instantiate physical item in the game world if(item.gameObject != null) { Instantiate(item.gameObject, storeRoomTransform.position, Quaternion.identity); } // 3. Logic: Add to dictionary if (!inventory.ContainsKey(item.itemName)) { inventory[item.itemName] = 0; } inventory[item.itemName]++; Debug.Log($"Purchased {item.itemName}"); // 4. Update Button Feedback (Optional) TMP_Text btnText = button.GetComponentInChildren(); if (btnText != null) { // Temporarily show "Bought" or keep price? // Usually shops keep the price so you can buy again. // If you want it to flash green, you'd need a Coroutine, // but for now let's keep it simple. } // 5. Refresh UI UpdateAllUI(); } else { Debug.Log("Not enough money!"); } } void UpdateAllUI() { // Update Money Text moneyText.text = "Money: $" + currentMoney; // Update Button Interactability for (int i = 0; i < buyButtons.Length; i++) { if (i < availableItems.Length) { buyButtons[i].interactable = (currentMoney >= availableItems[i].itemPrice); } } // Update Inventory List UpdateInventoryUI(); } void PopulateInventoryDictionary() { foreach (Item item in availableItems) { if (!inventory.ContainsKey(item.itemName)) { inventory[item.itemName] = 0; } } } void UpdateInventoryUI() { // 1. Clear existing list items foreach (Transform child in inventoryUI) { Destroy(child.gameObject); } // 2. Create new list items from the Template foreach (KeyValuePair entry in inventory) { // Only show items we actually have (Count > 0) if(entry.Value > 0) { // Instantiate the Template (which has the TMP_Text component) GameObject newItem = Instantiate(inventoryTextTemplate, inventoryUI); // Get the text component and set the string TMP_Text textComp = newItem.GetComponent(); if (textComp != null) { textComp.text = $"{entry.Key}: {entry.Value}"; } } } } }