• 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 ronronmx · Sep 27, 2012 at 04:38 AM · guiraycastingrect

Return BoxCollider Rect values in screen coordinates

Is there a way to get the Rect() values of a BoxCollider in GUI coordinates? For example:

 public Collider button01;
 private Rect button01Rect;
 
 void Update()
 {
     button01Rect =/* calculate Rect values from collider? */ button01.GetRect(); // would be nice :)
     if( button01Rect.Contains( Input.mousePosition )
     {
         // do stuff
     }
 }

I would like to be able to return any Collider's Rect() GUI coordinates, that way I can assign collider references, get their Rect, and use rect.Contains() instead of having to Raycast stuff...

Thanks for your time guys!

Stephane

Comment

People who like this

0 Show 4
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 ronronmx · Sep 27, 2012 at 04:15 PM 0
Share
  1. It's actually flat planes, with textures on them, and no I guess it won't always be squared-up with the screen.

  2. It's for a 2D GUI, on top of a 3D game.

  3. I thought that maybe Raycasting would be more expensive.

I tried converting the bounding box position to screen coords before, but I must have been doing it wrong because it wasn't giving me the same exact position as the collider's bounding box.

The only reason I want to do this is because I started with a class which had all the needed Rect() already defined, so it was easy:

public Rect btnRect; public Rect brakeRect;

void OnGui() { foreach( Touch touch in Input.touches ) { if( btnRect.Contains( touch.position ) { // Do stuff }

   if( brakeRect.Contains( touch.position )
   {
      // Do other stuff
   }

} } But I then switched to using NGUI for my UI/HUD, which uses planes w/textures, and I wanted to keep my old class the same, using rect.Contains(), so I thought I would just assign the NGUI buttons to variables in my old class, and get their Rect() values, that way I don't have to change the way the old class works.

Stephane

avatar image Bunny83 · Sep 27, 2012 at 04:26 PM 0
Share

Umm, if it's a 2D GUI it actually is "squared-up" with the screen, otherwise it's a 3D GUI. That's a bit confusing.

Raycasts aren't really expensive. It also depends on if you need the raycast every frame or only in certain situations (for example when the mouse has been clicked or a touch has happened). But even when you raycast every frame, it's not that bad.

btw, doesn't NGUI have it's own functions to test this? I never used it, but it's main purpose is to provide GUI functions which includes a "mouseover".

avatar image ronronmx · Sep 27, 2012 at 09:19 PM 0
Share

Got it for the 2D/3D "squared-up" thing...lol

I only need to raycast when a touch has happened, so I know which HUD button the touch happened on, and then call the proper event.

Yes NGUI does have its own functions, like OnPress(), OnDrag() etc...but since I already had my whole controls class done with touch.Phases to handle all required touch events, I didn't want to change/rewrite it to use NGUI's events. I'll do that for new HUD/Screens I need to create :)

From the looks of it, raycasting sounds like the easiest, quickest solution.

Thanks for the help guys!

avatar image ronronmx · Sep 29, 2012 at 05:54 AM 0
Share

Yeah, NGUI is difficult to get into, as the tutorials only deal with the most simple scenarios, and nothing really explains the API very clearly...I had to look at the source code to understand how some things worked. Once you know the ins and outs though, it's pretty sweet :)

But, I still feel more comfortable using my own classes, even if they aren't as powerful/well written/etc...I'll check into 2DToolkit!

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

'Button' is not a member of 'GUI' 1 Answer

Re Enable C# script on camera 1 Answer

Create GUI button in update function? 3 Answers

Texture Importer 3 Answers

targeting desnsity inside buoyancy script 0 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