Roman Peterson
Roman Peterson

Reputation: 91

Terminate drag while mouse is still down

Imagine some standard Unity UI ScrollView. You grab it with your mouse and start dragging it so it is scrolling with your mouse. How do I programmatically make it stop scrolling while mouse button is still down? Is there any way to make ScrollRect think that user is released mouse button?

Upvotes: 0

Views: 881

Answers (3)

Artur
Artur

Reputation: 11

You can call ScrollRect's OnEndDrag() method with artificial PointerEventData.

            var forceEndDragData = new PointerEventData(EventSystem.current)
            {
                button = PointerEventData.InputButton.Left
            };
            _scrollRect.OnEndDrag(forceEndDragData);

Upvotes: 1

The Tosters
The Tosters

Reputation: 417

Quite old question, but I found exactly this same problem and no clean solution. My problem was to have scrollRect with items which I can scroll left/right but when user select one item and drag it up then scrollRect should cancel scrolling and allow user to drag item up. I achieved this by temporary setting ScrollRect.enable to false, and after few ms set it back to true.

Here is part of mu code which do this. Maybe somebody find this useful.

void Update()
{
    if (state == DragState.Started) {
        dragStart = Input.mousePosition;
        state = DragState.Continued;
    }

    if (state == DragState.Continued) {
        Vector3 pos = Input.mousePosition;
        if (pos.y - dragStart.y > 80) {
            scrollRect.enabled = false;
            state = DragState.None;
            Invoke("ReenableScroll", 0.01f);
        }
    }
}

private void ReenableScroll() {
    scrollRect.enabled = true;
}

Upvotes: 1

zambari
zambari

Reputation: 5035

Its not easy to make the EventSystem think something has happend while it hasn't. I believe its much easier to implement your own ScrollRect logic and parse the mouse input yourself

Upvotes: 1

Related Questions