Ejemplo n.º 1
0
    /// <summary>
    /// 拾取到id的物品,并添加到物品栏里面
    /// 处理拾取物品的功能
    /// </summary>
    /// <param name="id"></param>
    public void GetId(int id)
    {
        //第一步 在所有的物品中是否存在该物品
        //第二 若存在,则物品num+1
        //第三 若不存在,查找空的方格,然后把新创建的inventoryItem放到这个空的方格里面
        InventoryItemGrid grid = null;

        foreach (InventoryItemGrid temp in itemGridList)
        {
            if (temp.id == id)
            {
                grid = temp;
                break;
            }
        }
        //存在的情况
        if (grid != null)
        {
            grid.PlusNum();
        }
        //不存在的情况
        else
        {
            foreach (InventoryItemGrid temp in itemGridList)
            {
                if (temp.id == 0)
                {
                    grid = temp;
                    break;
                }
            }
            //第三 若不存在,查找空的方格,然后把新创建的inventoryItem放到这个空的方格里面
            if (grid != null)
            {
                GameObject itemGo = NGUITools.AddChild(grid.gameObject, inventoryItem);
                itemGo.transform.localPosition         = Vector3.zero;
                itemGo.GetComponent <UISprite>().depth = 9;
                grid.SetId(id);
            }
        }
    }
Ejemplo n.º 2
0
    // 根据 id 拾取物品
    public void GetId(int id)
    {
        InventoryItemGrid grid = null;

        // 查找是否已有该物品
        foreach (InventoryItemGrid temp in itemGridList)
        {
            if (temp.GetId() == id)         // 已有
            {
                grid = temp;
                break;
            }
        }
        if (grid != null)
        {
            grid.PlusNum();                 // 该物品数目+1
        }
        else
        {
            // 查找是否有空格子
            foreach (InventoryItemGrid temp in itemGridList)
            {
                if (temp.GetId() == 0)      // 有空格子
                {
                    grid = temp; break;
                }
            }
            if (grid != null)               // 往空格子里放物品
            {
                GameObject itemGo = NGUITools.AddChild(grid.gameObject, inventoryItemPrefab);
                itemGo.transform.localPosition         = Vector3.zero;
                itemGo.GetComponent <UISprite>().depth = 4;
                grid.SetId(id);             // 改变各自装的物品
            }
        }
    }