Example #1
0
    //拖拽物品结束时调用的方法
    protected override void OnDragDropRelease(GameObject surface)
    {
        base.OnDragDropRelease(surface);
        if (surface != null)
        {
            if (surface.tag == Tags.inventory_item_grid)         //当拖放到一个空的格子中的时候
            {
                if (surface == this.transform.parent.gameObject) //把物体拖到自己的格子中
                {
                    ResetPosition();
                }
                else
                {
                    InventoryItemGird oldParent = this.transform.parent.GetComponent <InventoryItemGird>();

                    this.transform.parent = surface.transform;
                    ResetPosition();
                    InventoryItemGird newParent = surface.GetComponent <InventoryItemGird>();
                    newParent.SetId(oldParent.id, oldParent.num);

                    oldParent.ClearInfo();
                }
            }
            else if (surface.tag == Tags.inventory_item)    //当拖放到一个有物体的格子中的时候
            {
                InventoryItemGird grid1 = this.transform.parent.GetComponent <InventoryItemGird>();
                InventoryItemGird grid2 = surface.transform.parent.GetComponent <InventoryItemGird>();
                //InventoryItemGird grid3 = grid1;
                int id = grid1.id; int num = grid1.num;
                grid1.SetId(grid2.id, grid2.num);
                grid2.SetId(id, num);
                ResetPosition();
            }
            else   //当物体拖到格子以外的时候
            {
                ResetPosition();
            }
        }
        else
        {
            ResetPosition();
        }
    }
Example #2
0
    //拾取到ID的物品,并添加到背包,处理拾取的物体
    public void GetId(int id, int count = 1)
    {
        InventoryItemGird gird = null;

        //1.在背包中查找是否存在该物品,根据物品的ID在背包中查找物品
        foreach (InventoryItemGird temp in itemGirdList)
        {
            if (temp.id == id)
            {
                gird = temp;
                break;
            }
        }
        if (gird != null) /*背包中存在该物品*/
        //2.如果存在该物品,num+1;
        {
            gird.PlusNumber(count);
        }
        else   //背包中不存在该物品
        {
            foreach (InventoryItemGird temp in itemGirdList)
            {
                if (temp.id == 0)
                {
                    gird = temp;
                    break;
                }
            }
            //3.如果不存在,查找背包空的方格,然后把InventoryItem放到这个方格下面。
            if (gird != null)
            {
                GameObject itemGo = NGUITools.AddChild(gird.gameObject, inventoryItem);
                itemGo.transform.localPosition         = Vector3.zero; //添加的物品在格子的正中央。
                itemGo.GetComponent <UISprite>().depth = 8;            //物体显示的优先级
                //更新格子的信息
                gird.SetId(id, count);
            }
        }
    }