• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Pekuja · May 06, 2010 at 03:31 PM · gui

Preventing mouse clicks from passing through GUI controls

NOTE

this is issue is addressed most freshly here:

http://answers.unity3d.com/questions/784617/how-do-i-block-touch-events-from-propagating-throu.html


I have a GUI.Button in my game, but when I click on the button, the click also gets processed by another script; so I need a way to ignore mouse clicks in a script when the mouse is over a GUI button or other control.

http://answers.unity3d.com/questions/9328/gui-click-through asks the same question, but using the old GUIElement system. The accepted solution does not work with the immediate mode GUI system.

Comment
Tetrad
yoyo
DaveA
Ipsquiggle
Shadyfella13
SisterKy
maraoz
Maf
amzin7000
giorashc
Snikard
Langre
binarybehemoth

People who like this

13 Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image qJake · May 06, 2010 at 08:22 PM 0
Share

"The accepted solution does not work with the immediate mode GUI system." Can you explain what that means...?

avatar image Pekuja · May 07, 2010 at 04:26 PM 0
Share

Immediate mode is when you define your GUI in the OnGUI function, and thus calling the GUI functions every frame instead of defining them once, as you would in retained mode GUI, i.e. the old GUIText and GUITexture system.

avatar image Ipsquiggle · Dec 13, 2010 at 10:37 PM 0
Share

Has anyone had any insights to this since May?

avatar image SisterKy · Jul 22, 2011 at 12:31 AM 0
Share

cross-reference
http://answers.unity3d.com/questions/7531/if-i-have-a-button-over-my-3d-world-how-can-i-dete.html
http://answers.unity3d.com/questions/16587/gui-click-through.html

7 Replies

  • Sort: 
avatar image
Best Answer

Answer by Veehmot · May 30, 2012 at 10:08 PM

Use GUIUtility.hotControl.

... 2012


But be aware that "IsPointerOverGameObject" is, to some extent, the current (2015) solution to this absurd Unity problem.

http://answers.unity3d.com/questions/784617/how-do-i-block-touch-events-from-propagating-throu.htm

Comment
clunk47
dchen05
Leslie-Young
mikeschuld
Linus
Huacanacha
ima747
devluz
Maf
mtdrume
vikingnm
Hiten2012
binarybehemoth

People who like this

13 Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JesseRusak · Jul 07, 2012 at 03:04 PM 0
Share

That works great, thanks.

avatar image dchen05 · Apr 19, 2013 at 01:26 AM 0
Share

wow this is great. Makes it so simple. Good find.

avatar image Satscape · Aug 05, 2013 at 09:27 PM 1
Share

Thanks! The accepted answer is way more complicated than your solution... if (Input.GetMouseButtonDown(0) && GUIUtility.hotControl==0) { // Non-UI mouse click }

avatar image mikeschuld · Sep 21, 2013 at 10:09 PM 1
Share

I don't think box is actually considered a control in the classic sense. Controls are things that can take input events like keys (textboxes) and clicks (buttons sliders, checkboxes, etc)

avatar image brimock · Nov 03, 2013 at 05:12 PM 1
Share

This should be the accepted answer.

Show more comments
avatar image

Answer by Molix · May 07, 2010 at 05:21 PM

We encountered a similar problem, where raycasts and MouseOver/Up/Down were going "through" OnGUI windows and controls. Our solution is not terribly elegant, but once in place it works well and is easy to add to new OnGUIs.

We have a single flag (mouseOver2DGui) which is set to false each frame. Each OnGUI is responsible for setting the flag to true each frame, only if the mouse is over any of its own windows/controls. Then when raycasts perform, they just check if mouseOver2DGui first.

e.g. Each OnGUI does this (pseudo code):

mouseOverMe = myWindowRect.Contains(mousePos)
if( mouseOverMe ) SetMouseOver2dGUI()

SetMouseOver2dGUI is a static function that just sets the flag true. Make sure you clear the flag each frame. The raycasts/OnMouseOver/OnMouseDown just read the flag as needed (e.g. to ignore clicks if(mouseOver2DGui)).

Comment
Pekuja
user-8259 (google)
asafsitner
kolban
maraoz

People who like this

5 Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pekuja · May 08, 2010 at 12:41 PM 0
Share

That sounds ok. Can you be sure that no input code is run between resetting the flag and the OnGUI that sets it on again? I had this sort of problem when I tried to detect the click on the GUI and make that disable my regular mouse input code, but what happened was that the regular mouse input code was run before the OnGUI. I had my mouse input code in an Update though, not using the event system since that'd require me to have input components running on a whole bunch of different gameobjects, while currently I make do with a single one that does raycasts.

avatar image Molix · May 08, 2010 at 04:39 PM 0
Share

I don't have the code in front of me at the moment, but I think if you clear it in LateUpdate, that should be after all Update/OnGUI/MouseDown events.

avatar image Pekuja · May 11, 2010 at 01:22 PM 0
Share

It does seem like this solution works. I think the edge case where it doesn't work is if you click right when the cursor moves over the GUI, in which case Update gets the input first, with the flag not set, then LateUpdate resets the flag, and then OnGUI gets the input and sets the flag, but it's too late. This is very much an edge case though. 99% of users probably won't even encounter it, and even fewer will encounter it on a regular basis.

avatar image DaveA · Dec 07, 2010 at 08:24 PM 1
Share

Has Unity 3.0 addressed this at all? I have the similar problem: when my GUI text window is up, WASD keys still move the camera, and trying to drag the window also does mouselook (I have mouselook 'on' when left mouse is down). Does this mean I have to put this hack in my MouseLook and FPSWalker scripts too? There has to be an easier way....

avatar image DaveA · Dec 07, 2010 at 08:24 PM 0
Share

Would Event.Use() help here at all?

Show more comments
avatar image

Answer by QWERTY · May 06, 2010 at 08:28 PM

I don't know much about scripting but I did use this script for a game I was making. It was attached to 3d text.

function OnMouseEnter() { //change the color of the text renderer.material.color = Color.red; }

function OnMouseExit() { //change the color of the text renderer.material.color = Color.white; }

function OnMouseUp() { Health.LIVES = 3; Application.LoadLevel (3); }

The function OnMouseUp() is s only called if the mouse was pressed down while over the same GUIElement or Collider.

Here is a link to the Unity Script Reference http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseUp.html

Comment
Shadyfella13
clunk47
Joe_User

People who like this

1 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pekuja · May 06, 2010 at 09:33 PM 0
Share

Well, I guess using MouseUp events instead of MouseDown events might help since I would have the in between frames to disable the function if I detect a mousedown event on a GUI control. I'm not sure this actually solves the original problem though, as I'm not quite sure that a collider wouldn't receive these mouse events even if it was under a GUI control, and even if it were, this would require me to rethink how my input works as I'm currently not handling mouse events on specific gameobjects and colliders.

avatar image Shadyfella13 · Feb 25, 2011 at 02:29 AM 0
Share

This is not really helpful in this particular case because the terrain, and other GameObjects still receive these mouse events even though they are underneath a GUI control.

avatar image

Answer by 5parrowhawk_legacy · Jan 11, 2011 at 07:43 AM

Hi folks, This is kind of late, but I was working on the same problem and I think I've come up with a solution which addresses the problems above. The main drawback of my solution is that it requires you to put everything in a GUI.Window - thus you can't use odd-shaped "floating" buttons with it, unless you place the buttons on top of an invisible window. Also, it's a bit complicated but it should work.

The basic idea is this:

  1. We create a UI Manager object which contains a table of Window ID (int) to Window Rectangle (Rect) mappings. The easiest way to do this, I think, is to use a C# Dictionary<int, Rect> object.
  2. When showing the GUI, we register the GUI window rectangle with the UI manager. If we hide the GUI, we de-register the GUI window rectangle. Hence the UI manager always knows which areas of the screen are covered by GUI windows. (You may want to check and re-register the rectangle with the manager on every OnGUI call, for safety's sake - the advantage of using our C# Dictionary here is that it prevents us accidentally registering a window twice.)
  3. When processing a mouse event using Input.GetButtonDown(), we first poll the UI manager, which will check if the current mouse position is over a GUI window.

I'm pretty sure the overhead for this should be minimal - most likely 3 or 4 rectangle hit tests per frame - unless you have mouse event processing in many places, and many windows. It's a pain to set up but I expect it should work well.

Comment
Extrakun

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by overunity3d · Aug 06, 2011 at 04:05 AM

http://forum.unity3d.com/threads/96563-corrected-GUI.Button-code-%28works-properly-with-layered-controls%29?p=629284

Comment
clunk47

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Specify background color of Gui.Button at runtime 1 Answer

Delta-Time speeds up when pressing buttons 1 Answer

Create GUIText from Javascript 3 Answers

GUISkin or otherwise - Once clicked, button stays same color 1 Answer

Push down GUI everytime for loop cycles through Array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges