- Home /
Input.GetMouseButtonUp(0) not detected after drag
Hi! I'm working on an implementation of a toolbar where you drag the item from your inventory onto the previous rect that the GUI has painted. But I'm having difficulty getting unity to recognize mouse release. It seems to work fine with GetMouseButton() but of course I'd prefer on release than on drag. I can't seem to see why GetMouseButton() would differ from GetMouseButtonUp() in this instance. Any help is greatly appreciated!
var item = inventoryScript.GetDraggedItem();
if(Event.current.type == EventType.Repaint && GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition) && item != null){
print("Reaching here is fine!");
if(Input.GetMouseButtonUp(0)){
print("It never reaches here :(");
inventorySlot[number] = item;
}
/* This works, but is on drag instead of release
if(Input.GetMouseButton(0)){
inventorySlot[number] = item;
}
*/
}
Answer by syclamoth · Jan 25, 2012 at 01:31 AM
The problem is here:
if(Event.current.type == EventType.Repaint && GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition) && item != null)
key point: Event.current.type == EventType.Repaint.
Releasing the mouse creates a 'MouseUp' event, which is obviously not the same as a Repaint event. This means that the mouse can never be in the process of getting released at the same time as you are in a Repaint Event!
Change this to
if(Event.current.type == EventType.MouseUp && GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition) && item != null)
and use Event.button instead of Input.GetMouseButton, since you already know that the mouse was released.
In general, using Input.GetMouseButton inside of GUI code is a bad idea, because the GUI stuff gets called at a different rate and rhythm from the Update stuff that the Input class is designed to be used with.
Better still, you could skip the 'Event.current.type' stuff entirely, and just use
if (Event.current.button == 0)
{
// do clicky stuff
}
Oh I see, thanks.
I changed to EventType.$$anonymous$$ouseUp but it's not entering the conditional? It appears as though the current event seems to never be $$anonymous$$ouseUp?
Have you tried testing for just the $$anonymous$$ouseUp without testing the GetLastRect bit as well? It's possible that because of the way the GUILayout works, that part isn't working properly now.
Your answer
