Пример #1
0
 /// <summary>
 /// NGUI内置函数,放下拖拽的物品时会记录下物品放下位置下面的物体
 /// </summary>
 /// <param name="surface">物品放下位置下面的物体</param>
 protected override void OnDragDropRelease(GameObject surface)
 {
     base.OnDragDropRelease(surface);
     if (surface != null)
     {
         //如果放到空格子上
         if (surface.tag == "Grid")
         {
             //如果空格子是自己的格子
             if (surface == this.transform.parent.gameObject)
             {
                 ResetPos();
             }
             //如果是别的空格子(就把物品放过去同时清空自己的格子)
             else
             {
                 GridOfItem oldGrid = this.GetComponentInParent <GridOfItem>();
                 this.transform.parent = surface.transform;
                 ResetPos();
                 GridOfItem newGrid = surface.GetComponent <GridOfItem>();
                 newGrid.SetThisGridItemById(oldGrid.id, oldGrid.num);
                 oldGrid.ClearItem();
             }
         }
         //如果放到有物品的格子上,就交换两Grid中物品(由于item在Grid上层,所以surface是item)
         else if (surface.tag == "Item")
         {
             GridOfItem oldGrid = this.GetComponentInParent <GridOfItem>();
             GridOfItem newGrid = surface.GetComponentInParent <GridOfItem>();
             int        tempid  = oldGrid.id;
             int        tempnum = oldGrid.num;
             oldGrid.SetThisGridItemById(newGrid.id, newGrid.num);
             newGrid.SetThisGridItemById(tempid, tempnum);
             //避免交换后会认为原有item仍然处于hover状态导致infoFrame不会消失
             oldGrid.GetComponentInChildren <Item>().isHover = false;
         }
         //保留(放到技能栏上的情况)
         else if (false)
         {
         }
     }
     ResetPos();
 }
Пример #2
0
    /// <summary>
    /// 当捡取物品时,根据所捡物品id将其添加入背包中
    /// </summary>
    /// <param name="id">物品id</param>
    /// <param name="num">物品数量</param>
    public void PickOneItemById(int id, int num = 1)
    {
        //存储捡到的物品的id对应所在的Grid对象(分有无情况)
        GridOfItem findGrid = null;

        //遍历所有Grid判断是否已经存在该物品
        foreach (GridOfItem tempGrid in gridList)
        {
            if (tempGrid.id == id)
            {
                findGrid = tempGrid; break;
            }
        }
        //如果已经存在(物品数量 + num)
        if (findGrid != null)
        {
            findGrid.AddItemNum(num);
        }
        else        //如果不存在(查找空的[id为0]Grid,再创建一个新的item放进Grid[设为子物体])
        {
            //寻找id为0的Grid(空Grid)
            foreach (GridOfItem tempGrid in gridList)
            {
                if (tempGrid.id == 0)
                {
                    findGrid = tempGrid; break;
                }
            }
            //实例化一个item设为空Grid子物体
            if (findGrid != null)
            {
                GameObject createNewItem = NGUITools.AddChild(findGrid.gameObject, newItem);
                createNewItem.transform.localPosition         = Vector3.zero;
                createNewItem.GetComponent <UISprite>().depth = 2;
                findGrid.SetThisGridItemById(id, num);
            }
        }
    }