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 Ambrose998800 · Jan 02, 2019 at 04:47 PM · rectcontains

Rect.Contains not working

The documentation says:

 void Update()
     {
         Rect rect = new Rect(0, 0, 150, 150);
         if (rect.Contains(Input.mousePosition))
             Debug.Log("Inside");
     }

Seems legit & simple

Now my code:

alt text

The traget is recognized by OnPointerEnter event -> the pointer is in the rect for sure!

But:

alt text

Is that a bug!?

rectcontainsprint.png (12.8 kB)
rectcontainscode.png (6.6 kB)
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
1

Answer by JojoIV · Jan 02, 2019 at 07:14 PM

Sometimes the RectTransform.rect is a bit confusing and not always what you would expect (my experience). It depends on the canvas mode what you should do (maybe you have to translate either the pointer position or the rect). It may help you if you debug output the rectangle position and size as well as the pointer position to find out if this is causing the issue.

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 Owen-Reynolds · Jan 02, 2019 at 11:23 PM 1
Share

Yes, as per the manual, the RectTransform rect is in the parent's local space - how far the corner is from the parent item. Print it out and you'll see. Converting to absolute canvas coords is a real pain. I think most people use GetWorldCorners. But I think the normal way is a GraphicsRayCaster and you won't need to check rects.

avatar image Ambrose998800 Owen-Reynolds · Jan 03, 2019 at 04:00 PM 0
Share

Thanks for the hint with GetWorldCorners. Led me to RectTransformUtility.RectangleContainsScreenPoint that makes the job!

Any mention in the documentation would be useful. It seems very $$anonymous$$imalist...

avatar image Bunny83 · Jan 02, 2019 at 11:39 PM 0
Share

Right, Rect.Contains is an extremely simple and straight forward method. It works just like you would expect. If something doesn't work the way you expect it's because of wrong assumptions about your input data.


The RectTransform is part of the 2d and UI system of Unity. It works hand in hand with the EventSystem. A UI canvas could be in screen space or in worldspace. GUI space usually has it's origin at the top left while screen space generally has the origin at the bottom left. Input.mousePosition gives you the mouse position in screen space, not UI space or relative to a canvas.

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

100 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

Related Questions

Best way to check where touched 1 Answer

Problems with Rect 1 Answer

Rect contains question (regarding negative x,y values) 0 Answers

Rect.Contains in front of gameObject 0 Answers

Just fixed weird mouse over detection problem but not really sure how 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