- Home /
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
It's actually flat planes, with textures on them, and no I guess it won't always be squared-up with the screen.
It's for a 2D GUI, on top of a 3D game.
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
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".
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!
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!
Your answer
Follow this Question
Related Questions
'Button' is not a member of 'GUI' 1 Answer
raycast passing through joystick gui 0 Answers
How to create a gui that the user can write in and save? 2 Answers
Bool script error 1 Answer