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
3
Question by lazalong · Nov 25, 2011 at 09:03 AM · guiraycastmouseselect

How to NOT select an object behind a GUI?

Hey

I am selecting objects with raycasts but I would like to disable this behavior when the object is behind a GUILayout.

  • What is the best way to deal with this situation?

  • Is there a way to detect if the mouse is inside a GUILayout?

  • Should I put a GUITexture behind all my gui and use GUIElement.HitTest?

  • Or is using Rect.Contains() for each GUI windows the fastest way?

Note that I still want to be able to select the object if it is not behind the object so I can't simply disable the raycast when the gui is displayed.

Comment
Add comment
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 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by CHPedersen · Nov 25, 2011 at 10:02 AM

One of the other guys might have a better idea, but the solution I'd go with is the Rect.Contains one. In case you're wondering, when you're using GUILayout, you have to use GUILayoutUtility.GetLastRect().Contains() to get the most recently drawn rect to test for the mouse position. Since you can't get to arbitrary rects with GUILayout but only the most recently drawn one, it means the code that disables the raycast needs to be put in each OnGUI that draws a rect, over which raycasting needs disabling. This could be multiple places, unfortunately.

Comment
Add comment · Show 3 · 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 cj_coimbra · Nov 25, 2011 at 11:20 AM 0
Share

I also think Rect.Cointans() is a good way to go.

avatar image Tobias_J · Mar 09, 2012 at 07:27 PM 0
Share

I disagree. Seems to me it's just the least bad solution.

With such an emphasis on the difference between game surface and GUI, the engine should make this much, much easier.

avatar image CHPedersen · Mar 12, 2012 at 07:47 AM 0
Share

I agree, actually. It is the least bad solution. But unfortunately, Unity's GUI system has quite a few shortco$$anonymous$$gs, most of which probably stem from the fact that real-time computer graphics is inherently an immediate-mode state machine. It's really hard to build something that resembles and behaves like a proper OO GUI system around that. So often, you get code that tends to look messy and requires strict discipline to not get out of hand.

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

6 People are following this question.

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

Related Questions

Detecting a GUI element under the mouse? 1 Answer

Raycast needs to hit GUI 3 Answers

OnMouseUp() not working like meant 1 Answer

C# mouse Raycast question 3 Answers

Move selected object. 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