• 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 wooolly · Aug 13, 2017 at 07:34 AM · uiguiraycastdetection

Detecting game object under UI

Hello,

I need to detect whether my game object is under a UI element or not (New UI, with a canvas. Not legacy UI). Is this possible, and if so, how could I achieve it?

I tried using ray casting, but then I learnt that ray casting doesn't work for UI elements (or so I think so).

Thanks!

Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image

Answer by AdmiralThrawn · Aug 13, 2017 at 08:38 AM

Hey mate,

one possible solution I can think of is to disable the Graphic Raycaster that you should have together with your Canvas object.

The steps you could try out:

  1. Whenever a mouse click happens disable that raycaster object (have a reference to it)

  2. Run your detection routine to check if a game object has been clicked (due to 1. it should now be detected)

  3. If an object was hit, determine the xy-coordinates of your UI object. (Else, skip 3. to 5.)

  4. Compare click-coordinates (in screen space) with xy-coordinates from 3.

  5. Reenable the Graphic Raycaster in order to make your UI responsible again.

Let me know this helps.

alt text


graphicraycaster.png (28.5 kB)
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
avatar image

Answer by wooolly · Aug 13, 2017 at 06:03 PM

Hey @AdmiralThrawn

I appreciate the response. However, I'm guessing this only works with the mouse? I don't actually want to use it for the mouse, I just need to know whether the GameObject was under the UI at any point during execution. Thanks though.

I ended up resolving my issue in the end by using a method I found online. I modified the method slightly to suit my own needs, and this is the method I'm using.

 private bool IsPointInRT(Vector2 point, RectTransform rt)
     {
         // Get the rectangular bounding box of your UI element
         Rect rect = rt.rect;
 
         // Get the left, right, top, and bottom boundaries of the rect
         float leftSide = rt.anchoredPosition.x;
         // The below width is multiplied by 1.3f just to expand the width a slight bit, because it doesn't seem to measure properly (cuts short)
         float rightSide = rt.anchoredPosition.x + (rect.width*1.3f);
         float topSide = rt.anchoredPosition.y + rect.height;
         float bottomSide = rt.anchoredPosition.y;
 
         //Debug.Log(leftSide + ", " + rightSide + ", " + topSide + ", " + bottomSide);
 
         // Check to see if the point is in the calculated bounds
         if (point.x >= leftSide &&
             point.x <= rightSide &&
             point.y >= bottomSide &&
             point.y <= topSide)
         {
             return true;
         }
         return false;
     }

Note: Before deciding the use this method, I stumbled across RectTransformUtility.RectangleContainsScreenPoint - however, I tested that method provided by Unity and it didn't seem to work. I don't know whether I was using that method correctly or not, but it didn't seem to be returning very sensible results so I'd advise to all to use the method above instead (though you may need to change it slightly for your own needs, depending on how you use anchors etc). A link to the original method: https://stackoverflow.com/questions/40566250/unity-recttransform-contains-point

Comment

People who like this

0 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 AdmiralThrawn · Aug 13, 2017 at 09:23 PM 0
Share

Nice! Following the KISS principle, the simple solution should always be the preferred choice! :)

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

161 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 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 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 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 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 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 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 check if the Canvas is infront of an object 1 Answer

New UI - Ignore raycast where alpha is 0 2 Answers

Find what word is selected by mouse in GUI.Label 0 Answers

4.6.1 GUI. Get Pos Y property from script? 1 Answer

Image overlay in center of screen 2 Answers


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