예제 #1
0
    InventorySlotUI AddSlot()
    {
        InventorySlotUI slot = Instantiate(slotPrefab, transform);
        Navigation      customNav = slot.GetComponent <Button>().navigation;
        Navigation      leftNav, upNav;
        int             index = slots.Count;
        int             y     = index / columns;
        int             x     = index % columns;

        if (y == 0)
        {
            customNav.selectOnUp = interactableUp;
            upNav = interactableUp.navigation;
            if (x == 0)
            {
                upNav.selectOnDown        = slot.GetComponent <Button>();
                interactableUp.navigation = upNav;
            }
        }
        else
        {
            customNav.selectOnUp = getSlot(x, y - 1).GetComponent <Button>();
            upNav = getSlot(x, y - 1).GetComponent <Button>().navigation;
            upNav.selectOnDown = slot.GetComponent <Button>();
            getSlot(x, y - 1).GetComponent <Button>().navigation = upNav;
        }

        if (x == 0)
        {
        }
        else
        {
            customNav.selectOnLeft = getSlot(x - 1, y).GetComponent <Button>();
            leftNav = getSlot(x - 1, y).GetComponent <Button>().navigation;
            leftNav.selectOnRight = slot.GetComponent <Button>();
            getSlot(x - 1, y).GetComponent <Button>().navigation = leftNav;
        }

        slot.GetComponent <Button>().navigation = customNav;
        slots.Add(slot);
        slot.SetSlot(index, panel);
        return(slot);
    }
예제 #2
0
    public override void CreateMenu(RectTransform Parent)
    {
        Entity_Player player     = Entity_Player.Main;
        Vector2       startPoint = new Vector2(0, -Parent.rect.height * 0.5f);

        // Add callback
        mInventory = player.mInventory;
        mInventory.mChangeEvent += OnInventoryChange;

        // Create player hotbar
        uint  width     = Entity_Player.InventoryWidth;
        uint  height    = Entity_Player.InventoryHeight;
        float halfWidth = width * 0.5f;

        mInventorySlots = new InventorySlotUI[width, height];


        for (int x = 0; x < width; ++x)
        {
            for (int y = 0; y < height; ++y)
            {
                // Inventory slot
                InventorySlotUI slot = Instantiate(InventorySlotUI.DefaultSlot, Parent);
                slot.name = "Slot " + x + "," + y;
                slot.Construct(mInventory, x, y);

                // Make sure inventory is in order from top-left to bottom-right
                int displayY = y;
                if (y != 0)
                {
                    displayY = (int)height - y;
                }

                RectTransform slotRect = slot.GetComponent <RectTransform>();
                slotRect.localPosition = startPoint + new Vector2(slotRect.rect.width * (x - halfWidth + 0.5f), slotRect.rect.height * (displayY + 0.5f));


                // Set hotbar different colour
                if (y != 0)
                {
                    slot.SetColour(Color.grey);
                }


                // Set contents
                slot.SetItem(player.mInventory.Get(x, y));

                mInventorySlots[x, y] = slot;
            }
        }
    }