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 OnMouseButtonDown(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".
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.
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. My thanks to aliakbo!
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.
Parent a Plane to the camera, with the plane about 0.1 in front of the camera, then scale the plane so it fits behind the button that you don’t want anything clickable from behind. Optionally, unclick the plane’s Mesh Renderer to hide the plane, but retain the Collider…
Honestly, Unity should have a simple toggle box on all GUITextures to make it Transparent (overlappable clicks) or Opaque.
This one works with the new GUI elements, buttons, labels, etc.
Just check _isMouseBlocked() before performing your selection action and return from the method before performing your action.
using UnityEngine.EventSystems
public void RaytraceHitMe(GameObject obj)
{
if (_isMouseBlocked() ) { return; }
var sb = GetComponent<MySelectionBehaviour>();
sb.selectObject(obj);
}
private bool _isMouseBlocked()
{
return (EventSystem.current.IsPointerOverGameObject());
}
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 OnMouseDown 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.
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.
– anon61858128If 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 OnMouseButtonDown(0) event from other scripts.
– anon73820239Actually 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".
– StatementIn 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.
– Bovine