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
0
Question by CG-DJ · May 21, 2014 at 04:32 PM · guiraycastrtsselect

Raycast needs to hit GUI

Ok,

I'm making a RTS/Castle Builder. I want to be able to click on a building and a dialogue box to come up. I already have code that RaycastAlls where ever the mouse is and gets that position. I can click on the buildings and it works. That's not the problem.

The problem is I have no way of detecting if I'm hitting GUI. I'm using Daikon Forge of my GUI system. Right now, if I click on the GUI, I can still select an object behind the GUI. I want the GUI to be detected by the RaycastAll, so if I Raycast a GUI element I won't select a building. How can I get the GUI to be detected by a RaycastAll?

EDIT:

Thank you for all the great responses! I really appreciate it. But I went to Daikon Forge's forum and found out that they have a function to detect that: dfInputManager.ControlUnderMouse. Thanks anyway guys!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Cherno · May 22, 2014 at 03:27 AM

  1. Use GUIUtility.hotcontrol

  2. Call Event.current.Use immediately after any button is pressed

  3. Call return at the end of the button pressing code

  4. For non-click raycast blocking, only call raycasts if a boolean "overGUI" is set to false. Set it to true if the mouse cursor is inside a GUI Rectangle (using Rect.Contains). Set it to false in the LateUpdate function. Works perfectly.

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 Kiwasi · May 21, 2014 at 09:51 PM

GUIUtility.hotControl is the method I use. Its an int that tells you if the user is currently clicking on a control. Use an if statement to bypass your raycast code if hotControl !=0. Its more efficient not to do the raycast in the first place if you don't need it.

An alternative, but not as efficient method is to place a collider behind your GUI element. If that collider is hit you then ignore the rest of the raycast. Of course you have already paid the performance cost for the raycast at this point.

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
0

Answer by Xumbu · May 21, 2014 at 04:45 PM

you can't do that with Raycast.

you have to check every gui component when you click somewhere on the gui.

then set a boolean (for example 'mouseOnGui') to true.

when you select a object first check if 'mouseOnGui' is false.

I have written a gui system with component based mouseListeners etc.

that would make it realy easy. http://xumbu.org/unity3d/user-interface/doc

a newer version of my gui system even handles raycasting

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 CG-DJ · May 21, 2014 at 06:19 PM 0
Share

Does Daikon Forge support mouseOnGUI?

avatar image Xumbu · May 21, 2014 at 09:06 PM 0
Share

I never used Daikon Forge.

maybe you have to check it yourself in your source code

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to NOT select an object behind a GUI? 1 Answer

Making raycasts go to the centre of the screen 1 Answer

UI for Selected Buildings 1 Answer

Vertical distance indication 1 Answer

Unity RayCast Selection 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