Skip to content

Commit

Permalink
Sistema de Inventário - Sistema de Salvamento #35
Browse files Browse the repository at this point in the history
Sistema de Inventário - Sistema de Salvamento #35
  • Loading branch information
sousaawesley authored Jul 23, 2024
1 parent 71a1676 commit e2beb1a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Assets/Scripts/Item/InventoryItemController.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Item/InventoryItemController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions Assets/Scripts/Item/InventoryManager.cs
Original file line number Diff line number Diff line change
@@ -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<Item> Items = new List<Item>();
public Transform ItemContent;
public GameObject InventoryItem;
public Toggle EnableRemove;
public InventoryItemController[] InventoryItems;
private void Awake()
{
Instance = this;
Expand All @@ -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<Text>();
var ItemIcon = obj.transform.Find("ItemName").GetComponent<Image>();
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<InventoryItemController>();

for (int i=0; i < Items.Count; i++)
{
InventoryItems[i].AddItem(Items[i]);
}
}
}

0 comments on commit e2beb1a

Please sign in to comment.