diff --git a/Assets/Scripts/Item/InventoryItemController.cs b/Assets/Scripts/Item/InventoryItemController.cs new file mode 100644 index 0000000..8dd6ac2 --- /dev/null +++ b/Assets/Scripts/Item/InventoryItemController.cs @@ -0,0 +1,21 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + +public class InventoryItemController : MonoBehaviour +{ + Item item; + + public Button RemoveButton; + public void RemoveItem() + { + InventoryManager.Instance.Remove(item); + Destroy(gameObject); + } + + public void AddItem(Item newItem) + { + item = newItem; + } +} \ No newline at end of file diff --git a/Assets/Scripts/Item/InventoryItemController.cs.meta b/Assets/Scripts/Item/InventoryItemController.cs.meta new file mode 100644 index 0000000..d6e29b3 --- /dev/null +++ b/Assets/Scripts/Item/InventoryItemController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e7c0e83c7a121084bb05bbcf3c0c9508 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Item/InventoryManager.cs b/Assets/Scripts/Item/InventoryManager.cs index 25813ef..405b8e1 100644 --- a/Assets/Scripts/Item/InventoryManager.cs +++ b/Assets/Scripts/Item/InventoryManager.cs @@ -1,11 +1,17 @@ using System.Collections; using System.Collections.Generic; +using Unity.VisualScripting; using UnityEngine; +using UnityEngine.UI; public class InventoryManager : MonoBehaviour { public static InventoryManager Instance; public List Items = new List(); + public Transform ItemContent; + public GameObject InventoryItem; + public Toggle EnableRemove; + public InventoryItemController[] InventoryItems; private void Awake() { Instance = this; @@ -20,4 +26,50 @@ public void Remove(Item item) { Items.Remove(item); } + + public void ListItems() + { + //Clean + foreach (Transform item in ItemContent) + { + Destroy(item.gameObject); + } + + foreach (var item in Items) + { + GameObject obj = Instantiate(InventoryItem, ItemContent); + var itemName = obj.transform.Find("ItemName").GetComponent(); + var ItemIcon = obj.transform.Find("ItemName").GetComponent(); + var RemoveButton = obj.transform.Find("RemoveButton").gameObject; + + itemName.text = item.itemName; + ItemIcon.sprite = item.icon; + + if(EnableRemove.isOn) + RemoveButton.gameObject.SetActive(true); + } + + SetInventoryItems(); + } + + public void EnableItemsRemove() + { + if (EnableRemove.isOn) + { + foreach (Transform item in ItemContent) + { + item.Find("RemoveButton").gameObject.SetActive(false); + } + } + } + + public void SetInventoryItems() + { + InventoryItems = ItemContent.GetComponentsInChildren(); + + for (int i=0; i < Items.Count; i++) + { + InventoryItems[i].AddItem(Items[i]); + } + } }