Ejemplo n.º 1
0
    //拾取到id物品,并添加到物品栏里面
    public void AddItemToBag(string id, int count = 1)
    {
        InventoryItemGrid grid = null;

        //查找是否存在该物品,
        foreach (InventoryItemGrid temp in itemGrid)
        {
            if (temp.id == id)
            {
                grid = temp;
                break;
            }
        }

        //存在物品数量+1
        if (grid != null)
        {
            grid.PlusNumber(count);
        }
        else
        {
            //不存在,判断背包是否满
            foreach (InventoryItemGrid temp in itemGrid)
            {
                //当前格子id为空,则未满
                if (temp.id == "")
                {
                    grid = temp;
                    break;
                }
            }

            //背包未满,则创建物体
            if (grid != null)
            {
                InventoryItem item = grid.GetComponentInChildren <InventoryItem>();

                //如果不存在隐藏的Item则创建
                if (item == null)
                {
                    //NGUI的实例化
                    GameObject itemGo = grid.gameObject.AddChild(inventoryItem);
                    itemGo.transform.localPosition = Vector3.zero;

                    //格子深度为5,数量为7,物品为6
                    itemGo.GetComponent <UISprite>().depth = 6;
                    grid.SetId(id, count);
                }
                else
                {
                    item.GetComponent <UISprite>().enabled = true;
                    grid.SetId(id, count);
                }
            }
        }
    }
Ejemplo n.º 2
0
    protected override void OnDragDropRelease(GameObject surface)
    {
        base.OnDragDropRelease(surface);
        if (surface != null)
        {
            if (surface.tag == Tags.inventory_item_grid)
            {
                InventoryItemGrid oldItemGrid = gameObject.transform.parent.GetComponent <InventoryItemGrid>();
                InventoryItemGrid newItemGrid = surface.GetComponent <InventoryItemGrid>();
                if (newItemGrid != oldItemGrid)
                {
                    this.transform.parent = surface.transform;
                    newItemGrid.SetId(oldItemGrid.id, oldItemGrid.currentNumb);
                    oldItemGrid.ClearInfo();
                }
            }
            else if (surface.tag == Tags.inventory_item)
            {
                InventoryItemGrid oldItemGrid = gameObject.transform.parent.GetComponent <InventoryItemGrid>();
                InventoryItemGrid newItemGrid = surface.transform.parent.GetComponent <InventoryItemGrid>();

                int tempId  = newItemGrid.id;
                int tempNum = newItemGrid.currentNumb;

                newItemGrid.ClearInfo();
                this.transform.parent = newItemGrid.gameObject.transform;
                newItemGrid.SetId(oldItemGrid.id, oldItemGrid.currentNumb);

                oldItemGrid.ClearInfo();
                InventoryItem newItem = newItemGrid.GetComponentInChildren <InventoryItem>();
                newItem.gameObject.transform.parent = oldItemGrid.gameObject.transform;
                oldItemGrid.SetId(tempId, tempNum);
                newItem.ResetPosition();
            }
            else if (surface.tag == Tags.shortcut)
            {
                ShortcutGrid shortcutGrid = surface.GetComponent <ShortcutGrid>();
                shortcutGrid.SetInventory(itemId);
            }
            isHover = false;
        }
        ResetPosition();
    }