Would it be possible to reliably check for double clicks the the Input methods, or would that be frame rate dependent? In other words, if your game is only running at say 10 fps, would it miss out on double clicks that are too fast? Another question: The Event. methods - are they only for use within OnGUI?
These are general questions, but specifically I’m making an RTS game, and starting to code the mouse input for drag-selecting assets, etc.
OK, nobody wanted to answer those questions. Could somebody at least take a look at this c# code skeleton I made and let me know if I’m the right track?
void OnGUI() {
switch (Event.current){
case EventType.MouseDrag:
//process mousedrag here
Event.Use();
break;
case EventType.mouseDown:
if (Event.clickCount == 1){
//process single click
}
else if (Event.clickCount == 2){
//process double click
}
Event.Use();
break;
}
}
I Figured it out. This code works. Feel free to use it.
void OnGUI() {
Vector2 MousePos = Event.current.mousePosition;
switch ((int)Event.current.type){
case (int)EventType.MouseDrag:
//process mousedrag here
if (Event.current.button==0) {
//process left drag
}
else if (Event.current.button==1) {
//process right drag
}
Event.current.Use();
break;
case (int)EventType.mouseDown:
if (Event.current.button==0) { //left button
if (Event.current.clickCount == 1){
//process single left click
}
else if (Event.current.clickCount == 2){
//process double left click
}
}
else if (Event.current.button==1) { //right button
if (Event.current.clickCount == 1){
//process single right click
}
else if (Event.current.clickCount == 2){
//process double right click
}
}
Event.current.Use();
break;
}
}
I have a question along the same lines. Right now I have a cube and when someone clicks on it I want to know the x, y and z coordinates for the spot they clicked on.
I have one simple test about mouse input. I used both input and event way to catch mouse click, one insteresting thing happened, when I click on GUI, only the input.getmousebottondown catch the click, no UI EventType.MouseDown catched, if click on none GUI place, in my case, the 3d world, both catched.
Here’s another problem: on Input.mousePosition, the coordinates (0,0) are at the bottom-left of the screen. With Event.mousePosition, the top-left is at (0,0). Not only is that inconsistent, but the Event.mousePosition can’t be used with Camera.ScreenPointToRay() unless you mirror an axis first.
first of all OnGUI will be called each time you click on a gui and it’s not dipendent to FPS so you can have many more or less OnGUI functions compared to Update
so your clicks will effect your game completely even in 10FPS.
about event this class is just for unity GUI and don’t work in normal situations but Input.GetButtonDown or Input.MousePosition always work
about clicking on the cube you can use Camera.ScreeToRay to get exact position of mouse in 3D and you can do it inside OnMouseDown of the cube