startADrag()

private void startADrag(InputAction.CallbackContext context)
    {
        PointerEventData pointerData = new(EventSystem.current);
        pointerData.pointerId = -1;
        if (context.action.ToString() == "Land/MouseClick[/Mouse/leftButton]")
        {
            pointerData.position = trackMouseAction.ReadValue();
        }
        else
        {
            pointerData.position = new Vector2(context.ReadValue().x, context.ReadValue().y);
        }
        List results = new();
        EventSystem.current.RaycastAll(pointerData, results);
        int raycastCount = results.Count;
        if (raycastCount > 0)
        {
            foreach (var result in results)
            {
                Slot slot = result.gameObject.GetComponent();
                if (slot)
                {
                    if (slot.itemAmount > 0)
                    {
                        toDrag.SetActive(true);
                        float canvasSize = slot.fullItem.GetCanvasSize();
                        Sprite dragItem = slot.fullItem.GetSprite();
                        if (dragItem.bounds.extents.y > dragItem.bounds.extents.x)
                        {
                            float ratio = dragItem.bounds.extents.y / dragItem.bounds.extents.x;
                            toDrag.GetComponent().transform.localScale = new Vector3(canvasSize / ratio, canvasSize, canvasSize);
                        }
                        else
                        {
                            float ratio = dragItem.bounds.extents.x / dragItem.bounds.extents.y;
                            toDrag.GetComponent().transform.localScale = new Vector3(canvasSize, canvasSize / ratio, canvasSize);
                        }
                        toDrag.GetComponent().material = slot.fullItem.GetMaterial();
                        toDrag.GetComponent().sprite = dragItem;
                        beingDragged.currentItem = slot.fullItem;
                        Vector3 dragPos = Camera.main.ScreenToWorldPoint(pointerData.position);
                        toDrag.transform.position = new Vector3(dragPos.x, dragPos.y, 0);
                    }
                    break;
                }
            }
        }
    }


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *