Exemplo n.º 1
0
    void Update()
    {
        if (!canDrag)
        {
            return;
        }
        if (IsOnDrag)
        {
            Vector3 uiCameraPosition = UIManager.Instance.UICamera.ScreenToWorldPoint(Input.mousePosition);

            if (isBegin)
            {
                mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
            }

            switch (dragFrom)
            {
            case DragAreaTypes.MechaEditorArea:
            {
                if (isBegin)
                {
                    dragBeginPosition_WorldObject = GetMouseAsWorldPoint() + mOffset + new Vector3(0.5f, 0, 0.5f) * GameManager.GridSize;
                    oriPosition_WorldObject       = transform.localPosition;
                    oriQuaternion_WorldObject     = transform.localRotation;
                }

                caller.DragComponent_OnMousePressed(CheckMoveToArea());     //将鼠标悬停的区域告知拖动对象主体
                break;
            }

            case DragAreaTypes.Bag:
            {
                if (isBegin)
                {
                    dragBeginPosition_UIObject   = uiCameraPosition;
                    oriAnchoredPosition_UIObject = ((RectTransform)transform).anchoredPosition;
                }

                caller.DragComponent_OnMousePressed(CheckMoveToArea());     //将鼠标悬停的区域告知拖动对象主体

                float draggedDistance = (uiCameraPosition - dragBeginPosition_UIObject).magnitude;
                if (draggedDistance < caller.DragComponent_DragMinDistance)
                {
                    //不动
                }
                else if (DragManager.Instance.IsMouseInsideBag)     //拖拽物体本身
                {
                    if (RectTransformUtility.ScreenPointToLocalPointInRectangle(BagManager.Instance.BagPanel.ItemContainer.transform as RectTransform, Input.mousePosition, UIManager.Instance.UICamera, out Vector2 mousePos))
                    {
                        mousePos.x += ((RectTransform)BagManager.Instance.BagPanel.ItemContainer).rect.width / 2f;
                        mousePos.y -= ((RectTransform)BagManager.Instance.BagPanel.ItemContainer).rect.height / 2f;
                        int grid_X = Mathf.FloorToInt((mousePos.x) / BagManager.Instance.BagItemGridSize);
                        int grid_Y = Mathf.FloorToInt((-mousePos.y) / BagManager.Instance.BagItemGridSize);

                        int grid_X_delta = grid_X - DragManager.Instance.CurrentDrag_BagItem.GridPos.x;
                        int grid_Y_delta = grid_Y - DragManager.Instance.CurrentDrag_BagItem.GridPos.z;

                        if (grid_X_delta != 0)
                        {
                            bool suc = BagManager.Instance.BagPanel.CheckSpaceLocked(DragManager.Instance.CurrentDrag_BagItem.RealPositionsInBagPanel, new GridPos(grid_X_delta, 0));
                            if (suc)
                            {
                                int x = grid_X * BagManager.Instance.BagItemGridSize;
                                ((RectTransform)transform).anchoredPosition = new Vector2(x, ((RectTransform)transform).anchoredPosition.y);
                            }
                        }

                        if (grid_Y_delta != 0)
                        {
                            bool suc = BagManager.Instance.BagPanel.CheckSpaceLocked(DragManager.Instance.CurrentDrag_BagItem.RealPositionsInBagPanel, new GridPos(0, grid_Y_delta));
                            if (suc)
                            {
                                int y = grid_Y * BagManager.Instance.BagItemGridSize;
                                ((RectTransform)transform).anchoredPosition = new Vector2(((RectTransform)transform).anchoredPosition.x, -y);
                            }
                        }
                    }
                }
                else     //拖出背包
                {
                    caller.DragComponent_DragOutEffects();
                }

                break;
            }
            }

            isBegin = false;
        }
    }