Search Unity

[SOLVED] Multigrid Inventory

Discussion in 'Scripting' started by aranthel09, Jul 28, 2017.

  1. aranthel09

    aranthel09

    Joined:
    Jul 17, 2015
    Posts:
    66
    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
    Code (CSharp):
    1. void Start () {
    2.  
    3.         slotPanel.GetComponent<GridLayoutGroup> ().constraintCount = colunas;
    4.         if (maxSize % colunas == 0) {
    5.             linhas = maxSize / colunas;
    6.         } else {
    7.             linhas = (maxSize / colunas) + 1;
    8.         }
    9.  
    10.         for (int x = 0; x < linhas; x++) {
    11.             for (int y = 0; y < colunas; y++) {
    12.                 var slots = Instantiate (slotPrefab);
    13.                 slots.transform.SetParent (slotPanel.transform);
    14.                 slots.GetComponent<Slot> ().x = x;
    15.                 slots.GetComponent<Slot> ().y = y;
    16.                 slots.GetComponent<Slot> ().isEmpty = true;
    17.                 slotList.Add (slots);
    18.                 slots.name = x.ToString () + " , " + y.ToString ();
    19.                 if (slotList.Count == maxSize) break;
    20.      
    21.             }
    22.         }
    23.          
    24.     }
    25.  
    26. ...
    27. public bool hasSpace(GameObject item){
    28.  
    29.  
    30.         var itemReference = item.GetComponent<Item> ();
    31.         print ("ItemVolume: " + itemReference.volume);
    32.         for (int i = 0; i < slotList.Count; i++) {
    33.             int volume = 0;
    34.             Slot slot = slotList [i].GetComponent<Slot> ();
    35.             if (slot.isEmpty) {
    36.                 if (itemReference.x == 1 && itemReference.y == 1) {
    37.                     AddItem (item, slot);
    38.                     return true;
    39.                 } else {
    40.                  
    41.  
    42.  
    43.  
    44.  
     
  2. aranthel09

    aranthel09

    Joined:
    Jul 17, 2015
    Posts:
    66