UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"create an inventory system that is easy to customize from the Unity UI"
using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; public class Inventory : MonoBehaviour { public List<Item> items = new List<Item>(); public GameObject inventoryUI; public GameObject itemSlotPrefab; public Transform itemSlotPanel; void Start() { inventoryUI.SetActive(false); } void Update() { if (Input.GetKeyDown(KeyCode.I)) { inventoryUI.SetActive(!inventoryUI.activeSelf); UpdateUI(); } } void UpdateUI() { // Clear all existing slots foreach (Transform child in itemSlotPanel) { Destroy(child.gameObject); } // Create new slots for each item in the inventory foreach (Item item in items) { GameObject slot = Instantiate(itemSlotPrefab, itemSlotPanel); slot.GetComponentInChildren<Text>().text = item.name; slot.GetComponent<Button>().onClick.AddListener(() => UseItem(item)); } } void UseItem(Item item) { // Use the item Debug.Log("Used " + item.name); items.Remove(item); UpdateUI(); } } [System.Serializable] public class Item { public string name; public Sprite icon; }