Exemplo n.º 1
0
    public void DragEnd(PointerEventData data)
    {
        if (!dragging)
        {
            return;
        }

        dragging = false;

        BuildingBarUI bar = GetComponentInParent <BuildingBarUI>();

        // Work out the old index.
        float oldX     = oldPos.x;
        int   oldIndex = Mathf.RoundToInt((oldX - 5f) / 55f);

        // Make sure that the index is in bounds.
        oldIndex = Mathf.Clamp(oldIndex, 0, bar.Items.Count);

        // Work out newly placed index...
        float x     = ((RectTransform)transform).anchoredPosition.x;
        int   index = Mathf.RoundToInt((x - 5f) / 55f);

        // Make sure that the index is in bounds.
        index = Mathf.Clamp(index, 0, bar.Items.Count);

        if (index == oldIndex)
        {
            transform.position = oldPos;
            return;
        }

        if (index > oldIndex)
        {
            // Update the bar.
            index += 1;
            index  = Mathf.Clamp(index, 0, bar.Items.Count);
            bar.Items.Insert(index, Player.Local.BuildingInventory.GetItem(Prefab));
            bar.Items.RemoveAt(oldIndex);
            bar.Refresh();
        }
        else
        {
            // Update the bar.
            index = Mathf.Clamp(index, 0, bar.Items.Count);
            bar.Items.Insert(index, Player.Local.BuildingInventory.GetItem(Prefab));
            bar.Items.RemoveAt(oldIndex + 1);
            bar.Refresh();
        }
    }