Unity GUI:库存垂直显示一个插槽

时间:2017-08-19 10:56:10

标签: c# user-interface unity3d unity5

我在游戏中遇到问题。我创建了一个动态库存显示,它看起来像我希望它保存一件事:插槽的第一行高于库存边界一行:

enter image description here

这是我完成工作的方法:

private void CreateLayout()
{
    slotsList = new List<GameObject>();
    numEmptySlots = numSlots;

    // Set inventory spacing.
    inventoryWidth = (numSlots/numRows) * (slotSize + slotPaddingLeft) + slotPaddingLeft;
    inventoryHeight = numRows * (slotSize + slotPaddingTop) + slotPaddingTop;
    inventoryRect = GetComponent<RectTransform>();
    inventoryRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, inventoryWidth);
    inventoryRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, inventoryHeight);

    // Set dynamic row and column spacing.
    int numCols = numSlots/numRows;
    for (int y = 0; y < numRows; y++)
    {
        for (int x = 0; x < numCols; x++)
        {
            GameObject NewSlot = (GameObject) Instantiate(SlotPrefab);
            RectTransform newSlotRect = NewSlot.GetComponent<RectTransform>();
            newSlotRect.localPosition = inventoryRect.localPosition + new Vector3((slotPaddingLeft * x) + slotPaddingLeft + (slotSize * x),
                                                                                -(slotPaddingTop * y) - slotPaddingTop - (slotSize * y));
            newSlotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, slotSize);
            newSlotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, slotSize);
            slotsList.Add(NewSlot);
        }
    }
}

我的迹象在某处可能是错的,或者也许是我的起源?如果我在 - (slotSize * y)部分中将负号更改为正数,则插槽完全翻转到顶部正好,但如果我保留负数,则他们开始一个插槽的高度比他们应该显示的高,翻看看似错误的支点。

谢谢大家的帮助!

0 个答案:

没有答案