How to force refresh mouse position?

I have a piece of UI that can be dragged via pointer. It can be closed by clicking the cross button or by pressing Escape. When I move a pointer towards the draggable area, it changes, when I start dragging it changes again. And if I close my UI by pressing Escape key while dragging, my pointer will still look as if I am dragging something. So I need a way to somehow force Unity to recalculate my cursor position (send new raycast?) in OnDisable method of my UI component. I have been trying to find a way to do this but no results so far. Is there anyone who could give me a tip?


Are you saying the problem is the cursor still looks like it’s dragging something after you press escape?

If that’s the case, create a bool called isDragging set to true when you start dragging the UI. From there you just need to check if (Input.GetMouseButtonUp(0) == true && isDragging == true). There you want to change the cursor back to normal and set isDragging to false.

What you want are some clearly defined states for your cursor that you can transition between depending on what the cursor is currently doing. Jujo100 touches on a common idea, which is to use a Boolean value to keep track of whether something is happening or not.

Assuming you have an update loop where your cursor image is updated, you’d simply apply the correct image depending on which flag(s) are true. For instance, while your isDragging flag is true you would show the appropriate cursor image.

There are a lot of ways to do it, but at it’s root there’s some bit of code which is responsible for starting and stopping the drag activity; it’s at that point where you’d want to set the isDragging flag in your Cursor state machine. Depending on your setup you might be able to use unity’s animator controller for this, and simply set and unset properties of the controller in order to control which image is applied, but the basic idea is the same.