- Home /
Is there a built-in function to determine if any GUI item has been clicked?
What I need to figure out if there is a built-in method to determine if any GUI item has been clicked. For instance, I have a button in the HUD that you can click to open a status menu, but I'd like to be able to restrain other areas from acting on this click, such as the character firing their weapon, by making a call to the supposed built-in function to check for this click. Now, I know I could throw some sort of method within the block of code that handles button clicks to flag some boolean, but I would like to lean towards built-in methods if at all possible. So, to iterate, is there a built-in function to determine if any GUI item has been clicked?
Answer by Aeron0 · May 15, 2013 at 09:59 PM
enter code here
Did you look at the Event class in Unity?
var button : int Description: Which mouse button was pressed.
0 means left mouse button, 1 means right mouse button, 2 means middle mouse button. Used in EventType.MouseDown, EventType.MouseUp events.
You could maybe use this with
Rect customRect;
// Custom rectangle for the GUI area that you want clickable
void Start () {
customRect.x = 200;
customRect.y = 200;
customRect.width = 200;
customRect.height = 200;
}
void OnGUI()
{
GUI.Box(customRect,"Box");
// Check if mouse coordinates are in the rectangle
if (customRect.Contains(Event.current.mousePosition))
{
Event e = Event.current;
// Mouse is in the rectangle so check for clicks
if (e.button == 0 && e.isMouse){ Debug.Log("Left Click"); }
else if (e.button == 1) { Debug.Log("Right Click");}
else if (e.button == 2){ Debug.Log("Middle Click"); }
else if (e.button > 2){ Debug.Log("Another button in the mouse clicked"); }
}
}
}
Not exactly what I was hoping for (seems a little bulky), but it is indeed a built-in to deter$$anonymous$$e if a GUI item has been clicked. Thank you.
Answer by $$anonymous$$ · May 15, 2013 at 09:10 PM
You might be able to whip up something assigning GUI.tooltip (you dont have to actually display the tooltip) to the button in question and checking if its null where you fire a weapon, but there must be a better way to do this
What I'm looking for is a built-in function to check for this, if it exists. The code would look something like...
//Here, GUI.clicked is a hypothetical property that deter$$anonymous$$es if a GUI element was clicked or not. I need something of this nature, if it's out there.
if (Input.GetButtonDown("Fire1") && !GUI.clicked) {
//code for firing weapon
}
Ultimately, I'm looking to test for element clicks at the code that handles gunfire without having to write additional within each button or element that turns on a flag saying that the GUI has been clicked.
GUI.Button(new Rect(10, 10, 100, 20), new GUIContent("Button", "Test"));
//Here, GUI.clicked is a hypothetical property that deter$$anonymous$$es if a GUI element was clicked or not. I need something of this nature, if it's out there.
if (Input.GetButtonDown("Fire1") && GUI.tooltip != "Test") // for some reason GUI.tooltip == null always returns false for me, so I check it against a string
{
//code for firing weapon
}