• 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 Jason Hamilton · May 04, 2010 at 09:08 AM · guibutton

GUI click through?

NOTE for 2015...

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


Hi, in my game there is a panel in the corner that has buttons in it, but when I click the button it registers in the world, creating an object, can I stop this?

Comment
qJake
burnumd
Lipis
Pekuja
Ipsquiggle
Shadyfella13
SisterKy
maraoz

People who like this

8 Show 2
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 Ipsquiggle · Dec 13, 2010 at 10:34 PM 0
Share

Anyone found a solution for this yet?

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

cross-reference
http://answers.unity3d.com/questions/16774/preventing-mouse-clicks-from-passing-through-gui-c.html
http://answers.unity3d.com/questions/7531/if-i-have-a-button-over-my-3d-world-how-can-i-dete.html

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by qJake · May 04, 2010 at 09:51 AM

You're gonna want to take a look at the HitTest method.

You use it like this:

// C#
GUILayer l = Camera.main.GetComponent(typeof(GUILayer)) as GUILayer;
GUIElement ele = l.HitTest(Input.mousePosition);
if(ele != null)
{
     // The mouse is over a GUI element here. 
}
else
{
     // The mouse isn't over any GUI element here.
}
Comment
Jason Hamilton
Bampf
burnumd
Lipis
Cyclops
Orion 1
drawcode
sovalopivia
maraoz

People who like this

9 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 Jason Hamilton · May 04, 2010 at 10:17 AM 0
Share

thanks bro! helped a lot.

avatar image yoyo · May 06, 2010 at 06:24 AM 0
Share

That works for GUIText and GUITexture, but what about buttons and such created with UnityGUI?

avatar image qJake · May 06, 2010 at 08:11 PM 0
Share

It should work for any GUI element.

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

It works with any GUIElement, but the new UnityGUI system doesn't use those, and doesn't even require a GUILayer to work. You call GUI.Button every frame to create a button, for example.

avatar image Shadyfella13 · Feb 25, 2011 at 07:32 PM 2
Share

Any update on this in the last 10 months?

Show more comments
avatar image

Answer by FadeToBlack · Jun 03, 2012 at 10:23 AM

i made it through GUIUtility.hotControl

Comment
nicloay
Agostino

People who like this

2 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 Kehos · Dec 19, 2012 at 06:27 PM 0
Share

Thanks for the suggestion, you made my day!

avatar image

Answer by kz235 · Aug 13, 2014 at 10:14 PM

I added this to another thread and noticed this one... here is what I wrote there:

I just tried this trick and it seems to work great...

The Short Explanation:

Add a child object to your camera (box or plane). Add a collider to the object. Assign a material that is transparent, and a shader that supports transparent. Scale it far beyond what your camera would ever show. Shift it so it is just in front of the camera. As a child, it will now move with the camera. Deactivate it (uncheck in inspector). Now, when you show GUI stuff, call SetActive(true) on the object. When you are done with GUI stuff, call SetActive(false) on the object. Works like a charm on our system. We have two concurant cameras for objects and menu items. We do it with both cameras.

Detailed Steps (just coded from memory, but should work... C#):

1) Create a new material in unity. Give it the colors of ARGB 1,1,1,1 in the color picker screen. (0...255 color types).

That makes it pretty much transparent. I leave a little just to make sure it gets hit... ignorance regarding hit model, but WPF requires this also, hence passthrough clicks, etc.

For each Camera:

1) Create Box (you could use planes, but adjust other areas as needed), assign the material you created to it, and set the shader to Transparent/Diffuse

2) Add the box to the camera as a child. Deactivate the box (uncheck it next to the name of the object in the inspector).

3) Set it's position x,y,z to 0.

4) Set it's scale x,y,z to 10,7,0.1 respectively. (you may need to make it bigger in the x/y if you have huge angle views on your camera. Play around with it to get the size that works. If you zoom in and out, make sure it's big enough for the maximum zoom you’ll have.

5) Set it’s rotation x,y,z to 0.

6) Add a box collider.

7) If you have multiple of these, you may want to name them we use: ViewCollisionBlocker, MenuCollisionBlocker, etc.

8) Now, using the tree view, double click on the box to zoom to it. Then shift it out in front of the camera a little bit (0.1??? adjust as needed for your app, etc.).

9) On your script where you do the OnGUI() code… you may want to add

public GameObject []GUIBlockers;

to the script. Then, in the inspector on the script, add your blocker boxes to that array on the script adjusting the array size as needed.

10) On your script, you may also want to add..

private bool blockersActive = false;

10) In your OnGUI() section, you may want to add...

void OnGuid()

{

bool showBlockers = false;

// have logic here to determine if you are going to show gui stuff or not

// if you decide to show the gui...then set...

showBlockers = true;

// do your gui stuff

if(GUIBlockers != null)

{

if(showBlockers)

{

if(!blockersActive)

{

blockersActive = true;

foreach(GameObject blocker in GUIBlockers)

{

blocker.SetActive(true);

}

}

}

else if(blockersActive)

{

blockersActive = false;

foreach(GameObject blocker in GUIBlockers)

{

blocker.SetActive(false);

}

}

}

}

Comment

People who like this

0 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Javascript GUI.Button help, Error BCE0077 1 Answer

Adding Force with a GUI button 5 Answers

How do I destroy a GUI button when I click on it 1 Answer

GUI.Button Texture Problem - Simple? 2 Answers

Creating 4.6 ui button active zone bigger then Image within it 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