- Home /
Prevent a click from going through GUI buttons.
There are a couple questions about this, but all of them are old. The solutions seem super poor and hackish. Is there a good way of stopping a click event when it is used on a button these days?
Edit for clarity:
My specific problem is I let you select a virtual grid space in my game. It checks for mouse clicks in Update() and then raytraces from the camera. I have buttons on the screen which when clicked, also select the grid space that is behind them from my unrelated grid selection code.
I was hoping for an easier way to prevent the clicks on the button from registering in the Update().
From the responses there are apparently no non-hackish ways to accomplish this. My specific hack is I check if the cursor is in the rect defined by my buttons and don't run my code in Update if it is.
Does anyone here know if EZGUI solves this issue elegantly?
Is there a good way of stopping a click event when it is used on a button? yes don't put any code on the button, don't have it do anything, that's a good way of stopping a click event when it is used on a button. If however you want to be more specific with your question, someone here can provide you with an actual answer.
If you dont understand the question, dont act arrogant, it makes you look stupid. He means, is the click used on a GUI button possible to be used without triggering the On$$anonymous$$ouseButtonDown(0) event from other scripts.
Actually I find the question quite vague. I upvoted it, thinking I knew exactly what he meant. Now after reconsidering it, I am up to 3-4 different problems he could refer to. I think the question con benefit from more information on this question, something that is a bit more specific than "prevent a click from going through GUI buttons".
In EZGUI when I want to do this I place an invisible button that is disabled behind the GUI that overlays the main GUI so click-throughs cannot occur.
Answer by Extrakun · Mar 22, 2011 at 02:38 AM
You may have to implement a Finite State Machine for GUI and normal game components, and perhaps a master game state. When the game state detects that the mouse is over a GUI component, it will tells all game objects to go into a disabled state which would not react to any mouse click and when the mouse leave the GUI element, you will send a message to the master game state to inform that game objects can be enabled.
To detect mouse entering and leaving a GUI element, you'll have to declare a rectangle for it.
Probably not the best method for a small application with a few GUI elements. It would be helpful for a complex application with many GUI elements though.
Sounds like the way to go on a big game, but I'd be more interested in an easy workaround for small games. +1 for a good answer though. What I was thinking was adding an onmouseenter to stop the onmousedown event with a boolean flag.
Answer by aliakbo · Jan 26, 2014 at 09:24 AM
There is a much simpler solution here which suggests
if (Input.GetMouseButtonDown(2) && GUIUtility.hotControl == 0)
{
//will only click if not on GUI
}
That works well enough for buttons, but what about Boxes and Labels?
I can not believe how hard it was to find a realistic way to prevent gui button click throughs. Aliakbo's simple '&& GUIUtility.hotControl == 0' condition seems to have totally solved my problem in Unity 4.6 with GUI.Button. $$anonymous$$y thanks to aliakbo!
Answer by DaveA · Mar 22, 2011 at 02:49 AM
Yeah we did something like what Extrakun says. Made a singleton class that holds a variable that says 'GUI is up' and a LateUpdate that clears it. Then anything that puts up a GUI thing sets that variable (GUI is up) if the mouse is in bounds of the GUI element(s) and any scripts that would need to be 'blocked' check this variable (MouseLook, FPSWalker, etc). It's not pretty, but it's really just a couple of extra lines of code in each such script, not too bad.
Answer by Statement · Mar 22, 2011 at 02:31 AM
I hate to hand it to you, but the GUI is very hackish to start with so any workarounds probably are equally hackish.
I guess you are having some problem with things related such as drop down menues where buttons overlap other buttons. Right? And when you press one of the buttons in the drop down, it actually press some button that is under the drop down? It was quite some time since I did that kind of gui. The best advice I can give you is to design/work within the limitations of the gui and try to not break a leg.
Some heavy guesswork trying to remember how I solved this in the past:
Otherwise I think it is possible to do this if you make use of gui windows, and possibly they're required to be in separate game objects. It was something silly like that. I think it's a fault from immediate mode where objects are evaluated in the order they are declared in code. A button drawn under is called first, right?
GUI.Button("Under");
GUI.Button("Over");
So, the code seems to execute the Under rather than the Over button on a click. First come, first served.
Wait now I'm confused, did I misinterpret the question or did you? I think he meant: How do you have a click event (which you use for instance to shoot in a FPS) to be executed when the click happens when clicking a GUI button.
Well I think it's unclear what he says. I thought I'd fill in the other scenario about "Preventing a click from going through GUI buttons". It's up to the OP to decide which of the answers are correct. You're quite right there are many ways of interpreting this. But as I read it again, he mentions "Click Events" and I think now he means On$$anonymous$$ouseDown for objects in the world perhaps. Or it could be as simple as the player is shooting when he is pressing a button. I don't know, the question isn't very clear to me.
Answer by Bill 2 · Oct 13, 2011 at 02:44 AM
did what was outlined above and it's working well, so I though I would post some code. Make a script called GUIBlocker and add this
static var isBlocking:boolean=false;
function LateUpdate () {
isBlocking=false;
}
drag it on to an empty game object
then in your OnGUI functions of other scripts that open up GUI boxes that shouldn't be clicked "through", add this
ix = Input.mousePosition.x;
iy = Input.mousePosition.y;
var transMouse = GUI.matrix.inverse.MultiplyPoint3x4(Vector3(ix, Screen.height - iy, 1));
var panelRect:Rect=new Rect (x,y,w,h);
GUI.Label (panelRect,someTexture);
if(panelRect.Contains(Vector2(transMouse.x,transMouse.y))){
GUIBlocker.isBlocking=true;
}
then on your objects that handle mouse clicks but where you want to ignore clicks through gui boxes, say
if(!GUIBlocker.isBlocking){
//do stuff
}
Hope that helps
Your answer
Follow this Question
Related Questions
Blender like Camera Control Shortcuts 1 Answer
When does Drop UI event get called? 0 Answers
[4.6 GUI] Source of event 1 Answer
Input versus Event 1 Answer