Exemplo n.º 1
0
    /// <summary>
    /// 拾取到id的物品,并添加到物品栏里面
    /// 处理拾取物品的功能
    /// </summary>
    /// <param name="id"></param>
    public bool GetID(int id, int count = 1)
    {
        //查找在所有物品中是否有该物品
        //如果存在 count +1
        //不存在,查找空的方格,然后把新创建的BagItem放到这个空的方格中
        getSuccess = false;
        BagItemGrid grid = null;

        foreach (BagItemGrid temp in itemGridList)
        {
            if (temp.id == id)
            {
                grid = temp;
                break;
            }
        }
        if (grid != null)
        {
            grid.PlusCount(count);
            getSuccess = true;
        }
        else
        {
            foreach (BagItemGrid temp in itemGridList)
            {
                if (temp.id == 0)
                {
                    grid = temp;
                    break;
                }
            }
            if (grid != null)
            {
                GameObject itemGo = Instantiate(bagItem, grid.transform);
                itemGo.transform.localPosition = Vector3.zero;
                grid.SetByID(id, count);
                getSuccess = true;
            }
        }
        return(getSuccess);
    }