I’ve been trying to create a multigrid inventory. So far, it works if I try to insert a 1x1 item. However, I’m trying to figure the syntax in order to implement my logic. I know I need to check if the slots on the X axis and the Y axis are free, but I’m having trouble doing that. All my slots have a Slot.cs script inside them, with proper initialization, so, all of them have the correct values inside.
Edit: I’m using Grid Layout Group on my panel and setting the constraintCount to match the columns.
Inventory.cs
void Start () {
slotPanel.GetComponent<GridLayoutGroup> ().constraintCount = colunas;
if (maxSize % colunas == 0) {
linhas = maxSize / colunas;
} else {
linhas = (maxSize / colunas) + 1;
}
for (int x = 0; x < linhas; x++) {
for (int y = 0; y < colunas; y++) {
var slots = Instantiate (slotPrefab);
slots.transform.SetParent (slotPanel.transform);
slots.GetComponent<Slot> ().x = x;
slots.GetComponent<Slot> ().y = y;
slots.GetComponent<Slot> ().isEmpty = true;
slotList.Add (slots);
slots.name = x.ToString () + " , " + y.ToString ();
if (slotList.Count == maxSize) break;
}
}
}
...
public bool hasSpace(GameObject item){
var itemReference = item.GetComponent<Item> ();
print ("ItemVolume: " + itemReference.volume);
for (int i = 0; i < slotList.Count; i++) {
int volume = 0;
Slot slot = slotList [i].GetComponent<Slot> ();
if (slot.isEmpty) {
if (itemReference.x == 1 && itemReference.y == 1) {
AddItem (item, slot);
return true;
} else {