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 ?
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.
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");
}
}
Thank you this solved my problem!!
Thank you ! This solved my problem !!!