Exemplo 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);
                }
            }
        }
    }
Exemplo n.º 2
0
    /// <summary>
    /// 拾取到Id的物品,并添加到物品栏里面
    /// 处理拾取物品的功能
    /// </summary>
    /// <param name="id"></param>
    public void GetId(int id, int count = 1)
    {
        //第一步 查找在所有的物品中是否存在该物品
        //第二步 如果存在,让num +1;

        InventoryItemGrid grid = null;

        foreach (InventoryItemGrid temp in itemGridList)
        {
            if (temp.id == id)
            {
                grid = temp;
                break;
            }
        }

        //存在的情况
        if (grid != null)
        {
            grid.PlusNumber(count);
        }

        //不存在的情况
        else
        {
            foreach (InventoryItemGrid temp in itemGridList)
            {
                if (temp.id == 0)
                {
                    grid = temp;
                    break;
                }
            }
            if (grid != null)
            {
                //第三步 如果不存在,查找空的方格,然后把新创建的Inventory放到这个空的方格中;
                GameObject itemGo = NGUITools.AddChild(grid.gameObject, inventoryItem);
                itemGo.transform.localPosition         = Vector3.zero;
                itemGo.GetComponent <UISprite>().depth = 8;
                grid.SetId(id, count);
            }
        }
    }
Exemplo n.º 3
0
    // 拾取物品
    public void GetId(int id, int count = 1)
    {
        // 第一步查找是否存在该物品
        // 第二,存在num+1
        // 第三,不存在,查找空格,追加物品
        InventoryItemGrid grid = null;

        foreach (InventoryItemGrid temp in itemGridList)
        {
            // 已存在
            if (temp.id == id)
            {
                grid = temp;
                break;
            }
        }
        // 存在情况
        if (grid != null)
        {
            grid.PlusNumber();
        }
        // 不存在的情况
        else
        {
            foreach (InventoryItemGrid temp in itemGridList)
            {
                if (temp.id == 0)
                {
                    grid = temp;
                    break;
                }
            }
            if (grid != null)
            {
                // 不存在,查找空格,追加物品
                GameObject itemGo = NGUITools.AddChild(grid.gameObject, inventoryItem);
                itemGo.transform.localPosition         = Vector3.zero; // 初始位置,确保正中间
                itemGo.GetComponent <UISprite>().depth = 4;
                grid.SetId(id, count);
            }
        }
    }
Exemplo n.º 4
0
    public void GetId(int id, int number)
    {
        InventoryItemGrid grid = null;

        if (itemGridList.Count < 1 && number > 0)
        {
            GameObject itemGo = GameObject.Instantiate(InventoryItem);
            itemGo.transform.SetParent(this.transform);
            grid = itemGo.GetComponent <InventoryItemGrid>();
            grid.SetId(id, number);
            itemGridList.Add(grid);
        }
        else
        {
            foreach (InventoryItemGrid temp in itemGridList)
            {
                if (temp.id == id)
                {
                    grid = temp;
                    break;
                }
            }
            if (grid != null && number > 0)
            {
                grid.PlusNumber(number);
            }
            else
            {
                if (number > 0)
                {
                    GameObject itemGo = GameObject.Instantiate(InventoryItem);
                    itemGo.transform.SetParent(this.transform);
                    grid = itemGo.GetComponent <InventoryItemGrid>();
                    grid.SetId(id, number);
                    itemGridList.Add(grid);
                }
            }
        }
    }
Exemplo n.º 5
0
    //拾取id物品的功能,并添加到物品栏里面
    public void GetId(int id, int count = 1)
    {
        //第一步查找所有的物品是否存在该物品
        // 第二,如果存在,num+1
        InventoryItemGrid grid = null;

        foreach (InventoryItemGrid temp in itemGridList)
        {
            if (temp.id == id)
            {
                grid = temp;
                break;
            }
        }
        if (grid != null) //存在的情况
        {
            grid.PlusNumber(count);
        }
        else
        {
            //不存在的话,找个新的格子
            foreach (InventoryItemGrid temp in itemGridList)
            {
                if (temp.id == 0)
                {
                    grid = temp;
                    break;
                }
            }
            if (grid != null) //第三 查找新的方格,然后把新创建的 放到新的空格去
            {
                //添加空的物体
                GameObject itemGo = NGUITools.AddChild(grid.gameObject, inventoryItem);
                itemGo.transform.localPosition         = Vector3.zero;
                itemGo.GetComponent <UISprite>().depth = 4;
                grid.SetId(id, count);
            }
        }
    }
Exemplo n.º 6
0
    //拾取到物品,并添加到物品栏里面
    public void GetId(int id, int num = 1)
    {
        //第一步:查找在所有物品中是否存在该物品
        //第二步:如果存在,让numLabel+1
        //第三步:如果不存在,查找空的格子,然后创建新的Iventory-item放入里面
        InventoryItemGrid grid = null;

        foreach (InventoryItemGrid temp in itemGridList)
        {
            if (temp.id == id)
            {
                grid = temp;
                break;
            }
        }

        if (grid != null)  //存在的情况
        {
            grid.PlusNumber(num);
        }
        else  //不存在的情况
        {
            foreach (InventoryItemGrid temp in itemGridList)
            {
                if (temp.id == 0)
                {
                    grid = temp;
                    break;
                }
            }
            if (grid != null)
            {
                GameObject itemGo = NGUITools.AddChild(grid.gameObject, inventoryItem);
                itemGo.transform.localPosition         = Vector3.zero;
                itemGo.GetComponent <UISprite>().depth = 3;
                grid.SetId(id, num);
            }
        }
    }
Exemplo n.º 7
0
    /// <summary>
    /// 拾取到Id的物品,并添加到物品栏里
    /// </summary>
    /// <param name="id"></param>
    public void GetId(int id, int count = 1)
    {
        InventoryItemGrid grid = null;

        foreach (var item in itemGridList)
        {
            if (item.id == id)
            {
                grid = item;
                break;
            }
        }

        if (grid != null)
        {
            grid.PlusNumber(count);
        }
        else
        {
            foreach (var item in itemGridList)
            {
                if (item.id == 0)
                {
                    grid = item;
                    break;
                }
            }

            if (grid != null)
            {
                var tempItem = GameObject.Instantiate(inventoryItem);
                tempItem.transform.SetParent(grid.transform);
                tempItem.transform.localPosition = Vector3.zero;
                grid.SetId(id, count);
            }
        }
    }