Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
7
Question by Patyrn · Mar 22, 2011 at 01:23 AM · guievent

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?

Comment
Add comment · 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 AngryOldMan · Mar 22, 2011 at 02:12 AM 0
Share

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.

avatar image Joshua · Mar 22, 2011 at 02:36 AM 2
Share

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.

avatar image Statement · Mar 22, 2011 at 03:13 PM 1
Share

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".

avatar image Bovine · Aug 20, 2011 at 07:29 AM 0
Share

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.

7 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 1 · 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 Joshua · Mar 22, 2011 at 02:45 AM 0
Share

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.

avatar image
3

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
 }


Comment
Add comment · 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 Sprite101 · Feb 18, 2014 at 04:15 PM 1
Share

That works well enough for buttons, but what about Boxes and Labels?

avatar image Yo123 · Jul 13, 2016 at 02:30 PM 1
Share

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!

avatar image
2

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.

Comment
Add comment · 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
-1

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.

Comment
Add comment · 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 Joshua · Mar 22, 2011 at 02:44 AM 0
Share

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.

avatar image Statement · Mar 22, 2011 at 03:08 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · 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
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

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

ExecuteEvents.Execute pass object to from ui 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