InventoryManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryManager : MonoBehaviour
{
// 최대 중첩 횟수
public int maxStackedItems = 4;
public InventorySlot[] inventorySlots;
public GameObject inventoryItemPrefab;
// Default selectedSlot 번호
int selectedSlot = -1;
private void Start()
{
ChangeSelectedSlot(0);
}
private void Update()
{
if (Input.inputString != null)
{
bool isNumber = int.TryParse(Input.inputString, out int number);
if (isNumber && number > 0 && number < 8)
{
ChangeSelectedSlot(number - 1);
}
}
}
void ChangeSelectedSlot(int newValue)
{
// 선택된 인덱스가 0 이상인지 검사
if (selectedSlot >= 0)
{
inventorySlots[selectedSlot].Deselect();
}
inventorySlots[newValue].Select();
selectedSlot = newValue;
}
public bool AddItem(Item item)
{
// 모든 아이템 슬롯을 대상으로 진행
for (int i = 0; i < inventorySlots.Length; i++)
{
// i 번째 InventorySlot을 가져와서
InventorySlot slot = inventorySlots[i];
// 해당 Slot에 있는 InventoryItem을 가져옴
InventoryItem itemInSlot = slot.GetComponentInChildren<InventoryItem>();
// 가져온 아이템이 null이 아니고, 같은 아이템이고, 갯수가 4보다 작다면
// 아이템 갯수 증가
if (itemInSlot != null &&
itemInSlot.item == item &&
itemInSlot.count < maxStackedItems)
{
itemInSlot.count++;
itemInSlot.RefreshCount();
return true;
}
}
for (int i = 0; i < inventorySlots.Length; i++)
{
InventorySlot slot = inventorySlots[i];
InventoryItem itemInSlot = slot.GetComponentInChildren<InventoryItem>();
if (itemInSlot == null)
{
SpawnNewItem(item, slot);
return true;
}
Debug.Log(itemInSlot);
}
return false;
}
void SpawnNewItem(Item item, InventorySlot slot)
{
GameObject newItemGo = Instantiate(inventoryItemPrefab, slot.transform);
InventoryItem inventoryItem = newItemGo.GetComponent<InventoryItem>();
inventoryItem.InitialiseItem(item);
}
/*********************************************
추 가 된 부 분
**********************************************/
public Item GetSelectedItem(bool use)
{
InventorySlot slot = inventorySlots[selectedSlot];
InventoryItem itemInSlot = slot.GetComponentInChildren<InventoryItem>();
if (itemInSlot != null)
{
Item item = itemInSlot.item;
if (use == true)
{
itemInSlot.count--;
if (itemInSlot.count <= 0)
{
Destroy(itemInSlot.gameObject);
}
else
{
itemInSlot.RefreshCount();
}
}
return item;
}
return null;
}
}
TestGroup에 Get selected item 버튼 추가DemoScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DemoScript : MonoBehaviour {
public InventoryManagerinventoryManager;
public Item[] itemsToPickup;
public void PickupItem(int id) {
bool result = inventoryManager.AddItem(itemsToPickup[id]);
if(result == true) {
Debug.Log("Item added");
}
else {
Debug.Log("ITEM NOT ADDED");
}
}
public void GetSelectedItem(bool use) {
Item receivedItem = inventoryManager.GetSelectedItem(false);
if(receivedItem != null) {
Debug.Log("Received item: " + receivedItem);
} else {
Debug.Log("No item received!");
}
}
public void UseSelectedItem() {
Item receivedItem = inventoryManager.GetSelectedItem(true);
if (receivedItem != null) {
Debug.Log("Used item: " + receivedItem);
} else {
Debug.Log("No item used!");
}
}
}