Example #1
0
    public void DrawEquipment()
    {
        int equippableItemsSize = equipmentManager.GetEquipmentSize();

        nWidth  = 1;
        nHeight = equippableItemsSize;
        gridLayout.constraintCount = nWidth;                                                     //# of columns
        capacity = nWidth * nHeight;
        EquipmentItemButton[] childrenScripts = GetComponentsInChildren <EquipmentItemButton>(); //used to access the children game objects
        //Set or create each GameItem's UI element
        //TODO: understand how/why duplicate
        for (int location = 0; location < capacity; location++)
        {
            int[] gridLocation = ItemLocationInGrid(location);
            int   locX         = gridLocation[0];
            int   locY         = gridLocation[1];
            EquipmentItemButton childScript;
            if (location >= childrenScripts.Length) //Equipment panel grew in size; create more UI items.
            {
                GameObject child = (GameObject)GameObject.Instantiate(Resources.Load <GameObject>("Prefabs/" + itemUIPrefabLocation));
                child.transform.SetParent(gameObject.transform, false);
                childScript = child.GetComponent <EquipmentItemButton>();
                childScript.SetGUIReferences(this);
            }
            else
            {
                childScript = childrenScripts[location];
            }

            if (locX < 0 || locX >= nWidth || locY < 0 || locY >= nHeight)
            {
                Debug.Log("Set new item wrong params: (" + locX + "/ " + nWidth + ", " + locY + "/ " + nHeight + ")");
            }

            GameItem tmpItem = equipmentManager.GetEquippedItemFromIndex(locY);
            //TODO is this something to fix?
            if (tmpItem == null)
            {
                tmpItem = GameItem.UNSET_ITEM;
            }
            childScript.SetNewItem(tmpItem, locX, locY);
        }

        //If equipment panel shrank in size, delete extra UI slot elements
        for (int overIndex = capacity; overIndex < childrenScripts.Length; overIndex++)
        {
            GameObject.Destroy(childrenScripts[overIndex].gameObject);
        }
    }