I want my GUI to register a right click but it only seems to receive left click events? What gives?
So the first thing I found out was that none of the events/callbacks seem to work for the right mouse button. Also, it appears you can’t simply ask the GUITexture what it’s screen coordinates are. There is a function that comes close, but all sorts of weird things happen due the Y being zero at the bottom of the screen.
Here is my solution:
void OnGUI() {
Rect screenRect = guiTexture.GetScreenRect(); //This actually gets us most of the way there
//We need to invert the Y here or everything will be upside-down. We also need to offset by the height
Rect guiRect = new Rect (screenRect.x, Screen.height - screenRect.y - screenRect.height,screenRect.width,screenRect.height);
if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2)) {
//We get screwed on inverted Y again here
Vector2 mousePos = new Vector2(Input.mousePosition.x,Screen.height - Input.mousePosition.y);
//Finally we detect what button was clicked
if(guiRect.Contains(mousePos)) {
if(Input.GetMouseButton(0)) {
Debug.Log ("Left button clicked");
} else if(Input.GetMouseButton(1)) {
Debug.Log ("Right button clicked");
} else if(Input.GetMouseButton(2)) {
Debug.Log ("Middle button clicked");
}
}
}
}