私はC#を使用してUnity内でインベントリシステムを作成しました。私は現在、アイテムがスタックよりも大きい場合、それを現在のインベントリスロットに追加することをやめて、在庫に単純なスタックシステムを持っています。しかし、他のスロットがいっぱいになったら、同じアイテムのインスタンスを別のスロットに追加するように、どうすれば設定できますか?Unityインベントリシステムスタックシステム
ここに私のコードがありますので、私がもうクラスを共有する必要がある場合は、私に教えてください。どんな助けでも大歓迎です。ビューのために使用さ
/**
* Controller class used to handle the logic and functionality of the
inventory system
**/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventoryController : MonoBehaviour
{
Inventory inv;
ItemDatabase database;
public int maxSize = 3;
private void Awake()
{
database = GetComponent<ItemDatabase>();
inv = GetComponent<Inventory>();
inv.slotAmount = 25;
inv.background = GameObject.Find("SlotBackgroundPanel");
inv.slotPanel = inv.background.transform.Find("SlotPanel").gameObject;
for (int i = 0; i < inv.slotAmount; i++)
{
inv.items.Add(new InventoryModel());
inv.slots.Add(Instantiate(inv.inventorySlot));
inv.slots[i].GetComponent<ItemSlot>().id = i;
inv.slots[i].transform.SetParent(inv.slotPanel.transform);
}
}
public void AddItem(int id)
{
InventoryModel itemToAdd = database.FetchItemByID(id);
if (itemToAdd.Stackable && CheckItemInInventory(itemToAdd))
{
for (int i = 0; i < inv.items.Count; i++)
{
if (inv.items[i].ID == id)
{
ItemData data = inv.slots[i].transform.GetChild(0).GetComponent<ItemData>();
data.amount++;
data.transform.GetChild(0).GetComponent<Text>().text = data.amount.ToString();
break;
}
}
}
else
{
for (int i = 0; i < inv.items.Count; i++)
{
if (inv.items[i].ID == -1)
{
inv.items[i] = itemToAdd;
GameObject itemObj = Instantiate(inv.inventoryItem);
itemObj.GetComponent<ItemData>().inventoryModel = itemToAdd;
itemObj.GetComponent<ItemData>().amount = 1;
itemObj.GetComponent<ItemData>().slot = i;
itemObj.transform.SetParent(inv.slots[i].transform);
itemObj.transform.position = Vector2.zero;
itemObj.GetComponent<Image>().sprite = itemToAdd.Sprite;
itemObj.name = itemToAdd.Title;
break;
}
}
}
}
public void RemoveItem(int id)
{
InventoryModel itemToRemove = database.FetchItemByID(id);
if (itemToRemove.Stackable && CheckItemInInventory(itemToRemove))
{
for (int j = 0; j < inv.items.Count; j++)
{
if (inv.items[j].ID == id)
{
ItemData data = inv.slots[j].transform.GetChild(0).GetComponent<ItemData>();
data.amount--;
data.transform.GetChild(0).GetComponent<Text>().text = data.amount.ToString();
if (data.amount == 0)
{
Destroy(inv.slots[j].transform.GetChild(0).gameObject);
inv.items[j] = new InventoryModel();
break;
}
if (data.amount == 1)
{
inv.slots[j].transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = "";
break;
}
break;
}
}
}
else
{
for (int i = 0; i < inv.items.Count; i++)
{
if (inv.items[i].Title == null && inv.items[i].ID == id)
{
Destroy(inv.slots[i].transform.GetChild(0).gameObject); inv.items[i] = new InventoryModel();
break;
}
}
}
}
public void FullItem(int id)
{
InventoryModel itemIsFull = database.FetchItemByID(id);
if (itemIsFull.Stackable && CheckItemInInventory(itemIsFull))
{
for (int y = 0; y < inv.items.Count; y++)
{
if (inv.items[y].ID == id)
{
ItemData data = inv.slots[y].transform.GetChild(0).GetComponent<ItemData>();
data.transform.GetChild(0).GetComponent<Text>().text = data.amount.ToString();
if (data.amount >= maxSize)
{
inv.slots[y].transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = "3";
data.amount = 3;
Debug.Log(data.amount);
break;
}
}
}
}
}
public bool CheckItemInInventory(InventoryModel inventoryModel)
{
for (int i = 0; i < inv.items.Count; i++)
if (inv.items[i].Title == inventoryModel.Title)
return true;
return false;
}
}
クラスは
using System.Collections;
/**
* View class, used to handle the User Interface
**/
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public GameObject background;
public GameObject slotPanel;
InventoryController inventoryController;
public GameObject inventorySlot;
public GameObject inventoryItem;
public int slotAmount;
public enum type { diamond = 0, iron = 1, gold = 2, fuel = 3 };
public List<InventoryModel> items = new List<InventoryModel>();
public List<GameObject> slots = new List<GameObject>();
private void Start()
{
inventoryController = GetComponent<InventoryController>();
inventoryController.AddItem((int)type.diamond);
inventoryController.AddItem((int)type.diamond);
inventoryController.AddItem((int)type.diamond);
inventoryController.AddItem((int)type.diamond);
inventoryController.AddItem((int)type.gold);
inventoryController.AddItem((int)type.fuel);
inventoryController.FullItem((int)type.diamond);
}
}
クラスは
[
{
"id" : 0,
"title" : "Diamond",
"value" : 5,
"stats":{
"defence": 9,
"power" : 8,
"weight" : 5
},
"description": "Mined Diamond is stronger than metal",
"stackable" : true,
"rarity" : 10,
"slug": "Diamond"
},
{
"id" : 1,
"title" : "Iron",
"value" : 2,
"stats":{
"defence" : 5,
"power" : 6,
"weight" : 7
},
"description" : "Mined Iron is stronger than bronze",
"stackable" : false,
"rarity" : 6,
"slug" : "Metal"
},
{
"id" : 2,
"title" : "Gold",
"value" : 2,
"stats":{
"defence" : 0,
"power" : 0,
"weight" : 0
},
"description" : "Gold is valuable",
"stackable" : false,
"rarity" : 10,
"slug" : "Gold"
},
{
"id" : 3,
"title" : "Fuel2",
"value" : 2,
"stats":{
"defence" : 0,
"power" : 0,
"weight" : 0
},
"description" : "Fuel is needed for ship engines",
"stackable" : true,
"rarity" : 2,
"slug" : "Fuel2"
}
]
あなたのインベントリモデルに可変金額を設定し、 'if(inv.items [i] .ID == id && inv.items [i] .amount <= maxAmount)'のどこをチェックすることはできません。この在庫モデルが満杯かどうかを確認します。 – CNuts