- Home /
ignore left mouse button click in the game when using gui buttons
Hi I'm wondering if you can help me ? I have two gui GUI buttons created using "function OnGUI" How would I ignore mouse clicks in the game when clicking on the GUI buttons ?
Answer by karl_ · Oct 25, 2011 at 04:50 PM
Simply cordon off an area of screen and check against it when taking mouse input.
Example:
using UnityEngine; using System.Collections;
public class ignoreMouseInRect : MonoBehaviour { Rect guiArea = new Rect( 0, 0, 200, 100);
void OnGUI() { if(GUI.Button( guiArea, "Hello")) { Debug.Log("Clicked the Button!"); } }
void Update() { // Accept mouse input for game loop if(Input.GetMouseButtonDown(0) && !new Rect(guiArea.x, (Screen.height - guiArea.height), 200, 100).Contains(Input.mousePosition)) { Debug.Log("Clicked outside the button"); } } }
And in JS
var guiArea : Rect = new Rect( 0, 0, 200, 100);
function OnGUI() { if(GUI.Button( guiArea, "Hello")) { Debug.Log("Clicked the Button!"); } }
function Update() { // Accept mouse input for game loop if(Input.GetMouseButtonDown(0) && !new Rect(guiArea.x, (Screen.height - guiArea.height), 200, 100).Contains(Input.mousePosition)) { Debug.Log("Clicked outside the button"); } }
Answer by Taishan · Oct 22, 2012 at 11:06 AM
if (Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 0) { // do sth }
static var hotControl : int
Description The controlID of the current hot control.
The hot control is one that is temporarily active. When the user mousedown's on a button, it becomes hot. No other controls are allowed to respond to mouse events while some other control is hot. once the user mouseup's, the control sets hotControl to 0 in order to indicate that other controls can now respond to user input.
This! This is the solution! I would Upvote you, if I could. This solves all my problems!