Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Nice! Following the $$anonymous$$ISS principle, the simple solution should always be the preferred choice! :)

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

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

Simple text GUI 2 Answers

Problem with GUI element following enemy 0 Answers

UI Slider - making the touch area for slide larger than the UI image handle 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