- Home /
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?
Answer by Jujo100 · Mar 29, 2019 at 11:53 PM
Hi,
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.
Well, I am using IDragHandler, IBeginDragHandler, IEndDragHandler to manage this, so I don't have a variable and I don't really need one. But IEndDragHandler method won't run until I release L$$anonymous$$B, and I don't have to do so to close the UI via Escape button. And even if I release L$$anonymous$$B after closing the UI, the method will still not run because it is on the UI component that will be disabled on that moment.
Answer by iwaldrop · Mar 30, 2019 at 06:04 AM
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.
If you are talking about states, than Enum (maybe with [Flags] attribute) would be much more useful than a bool variable. What you are saying is pretty obvious, but I don't have an update loop. I am implementing IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler interfaces and change the cursor in corresponding methods. The thing is, for example, IPointerEnterHandler will trigger if cursor moved and entered some raycasting area, but it will not trigger if an object itself moved to the cursor. So if I start dragging cursor will change, and if I close my UI via Escape button without moving the cursor, neither of abovementioned events will fire. So what I need is a way to force Unity to behave as if I moved the cursor without actually moving it.
Your answer
Follow this Question
Related Questions
Make more GUI per int 1 Answer
How to fire gui button on mouse right release 2 Answers
IGUI IOS error 1 Answer
Distribute terrain in zones 3 Answers
Rotating a GUI Object Based on Mouse Position with Locked Mouse 1 Answer