- Home /
How do I register or read multiple MouseDown events ?
hey , I just creating a double click class which allows me to make anything clickable...
the code is written below . this is done just by checking i a mouse down event is triggered twice in under 0.25 seconds.
then i realised that while i am not able to "click" anything... it is not really clicking the way buttons handle clicks .
I am checking for a mouse up event over the GUI element , setting a float to be realtimeSinceStartup then checking for a mouse down event , check if realtimeSinceStartup - the previously saved realtimeSinceStartup is less than 0.25 thus completing the double click.
but no buttons in unities gui even registers a mouse up event .
this code does check for double clicks on GUI elements.. but not on GUI elements that already registers or read a MouseDown event somewhere in the unity API
public class Click
{
static float time2;
static bool result ;
public static bool DoubleClick(Rect rect)
{
bool result = false;
float time = Time.realtimeSinceStartup / 10.0f;
if (rect.Contains(Event.current.mousePosition))
{
if (Event.current.rawType == EventType.MouseUp)
{
time2 = time;
}
if (Event.current.rawType == EventType.MouseDown && time - time2 < 0.025f)
{
time2 = 0;
result = true;
}
}
return result;
}
}
Implementation example
private Rect someRect =(new Rect(100,100,100,50));
private Rect someOtherRect = new Rect(250,100,100,50)
private Texture2D = (Texture2D) Resources.Load("someimage",typeof(Texture2D));
void OnGUI()
{
GUI.Button(someRect,"double click me") ;
if(Click.DoubleClick(someRect)) Debug.Log("double clicked")
GUI.DrawTexture(someOtherRect ,image);
if(Click.DoubleClick(someOtherRect )) Debug.Log("texture clicked")
}
Answer by Bunny83 · Mar 19, 2015 at 05:44 PM
The "old" GUI system handles events with the Event class. In your code you use some strange class called CurrentRawMouseEvent. If a control actually reacts to an event it "eats" that event so no other element can see it or react to it. Since you use GUI.Button, it will eat the MouseDown / MouseUp events. When an even got "eaten" Event.current.type will return "EventType.Ignore" instead of the actual event. You can use "Event.current.rawType" to get the actual event type.
ps: using hardcoded 0.025 seconds is not a good ides. First of all it's pretty short. Usually the delay is longer. Also 0.025 will physically only work with at least 40 frames per second, though you would need more (80+) to guarantee that it's working.
Ahh, just saw that you divided your time by 10. So it's actually 0.25 and not 0.025 ^^
oooh i understand what you mean in regards to the 0.025 seconds
I replaced CurrentRaw$$anonymous$$ouseEvent is what it was
public static class Current$$anonymous$$ouseEvent
{
public static Event evt()
{
return (Event.current);
}
}
public static class CurrentRaw$$anonymous$$ouseEvent
{
public static EventType evt()
{
return (Current$$anonymous$$ouseEvent.evt().rawType);
}
}
umm does that mean that i no longer need to adjust the time dynamically to compensate for fps ?
thumbs up for the help and info :)
i'm still not getting the double click to work on things like DrawTexture if the DrawTexture rect if some other function using $$anonymous$$ouseDown is accessing the Rect of the DrawTexture .
i'm using rawType but i'm still having trouble .
after trying for a while , I guess it cant be done ... at least not easily .
Answer by hav_ngs_ru · Mar 19, 2015 at 05:01 PM
why dont you just use OnMouseUp/OnMouseDown event to register click and check time since last click?
speaking about your code... the question is WHERE and WHEN do you call your DoubleClick.DoubleClick() function?
your register first (and all others) click ONLY if you called DoubleClick from OnGUI when current event is MouseUp/MouseDown... do you really call it every OnGUI? in other words - its better to see a caller code.
this is a n implementation example
private Rect someRect =(new Rect(100,100,100,50));
void OnGUI()
{
GUI.Button(someRect,"double click me") ;
if(Click.DoubleClick(someRect)) Debug.Log("double clicked")
}
and I had to rename the class in the example . the class name is now Click and not DoubleClick
oh my I just kep messing up . sorry "CurrentRaw$$anonymous$$ouseEvent" is just Event.current.rawType