Ejemplo n.º 1
0
    //surface本质上传过来的是UICamera.hoveredObject,也就是图片下面的是那个ui物体
    protected override void OnDragDropRelease(GameObject surface)
    {
        base.OnDragDropRelease(surface);

        bool isCleanChildren = false;

        //拖放到空格子
        if (surface.tag == Tags.inventoryItemGrid)
        {
            //拖放到非自身的空格子
            if (surface != transform.parent.gameObject)
            {
                InventoryItem     hideItem = surface.GetComponentInChildren <InventoryItem>();
                InventoryItemGrid oldGrid  = transform.parent.GetComponent <InventoryItemGrid>();
                InventoryItemGrid newGrid  = surface.GetComponent <InventoryItemGrid>();

                //判断这个空格子是真的没有物体,还是有一个隐藏的InventoryItem
                if (hideItem == null)
                {
                    //必须把设置放在SetId之前,SetId会获取子组件
                    transform.parent = surface.transform;

                    newGrid.SetId(oldGrid.id, oldGrid.itemNum);

                    oldGrid.CleanInfo();
                }
                else
                {
                    hideItem.GetComponent <UISprite>().enabled = true;
                    newGrid.SetId(oldGrid.id, oldGrid.itemNum);

                    oldGrid.CleanInfoInChildren();
                    isCleanChildren = true;
                }
            }
        }
        //拖放到有物品的格子,两个格子信息交换
        else if (surface.tag == Tags.inventoryItem)
        {
            InventoryItemGrid originalGrid = transform.parent.GetComponent <InventoryItemGrid>();
            InventoryItemGrid targetGrid   = surface.transform.parent.GetComponent <InventoryItemGrid>();

            string id  = targetGrid.id;
            int    num = targetGrid.itemNum;

            targetGrid.SetId(originalGrid.id, originalGrid.itemNum);
            originalGrid.SetId(id, num);
        }
        //拖放到快捷栏
        else if (surface.tag == Tags.shortCut)
        {
            surface.GetComponent <ShortCutGrid>().SetInventoryItem(m_id);
        }

        ResetItemPosition();

        //因为如果清空格子的子物体代表会执行隐藏操作,这里把它显示有不合理
        if (isCleanChildren == false)
        {
            transform.parent.GetComponentInChildren <UILabel>().enabled = true;
        }

        sprite.depth = originalDepth;
    }