Hi, first time posting
I'm trying to get an NGUI object (and it's combined children 's widgets) bounds from world (or local) space to screen space, so that I can then test if an NGUI object is overlapping with another on the screen, using something like:
if(screenSpaceBounds[i].bounds.Intersects(screenSpaceBounds[j].bounds)
So to get the bounds I'm using:
Bounds labelBounds = NGUIMath.CalculateRelativeWidgetBounds(nextLabel.transform, false);
Then passing those bounds to a custom function to create screen space bounds:
Bounds WorldBoundsToSceenBounds(Bounds worldBounds)
{
//Calculate the forward facing rectangle points of the label
Vector3 bottomLeft
= worldBounds
.center + new Vector3
(-worldBounds
.extents.x,
-worldBounds
.extents.y, worldBounds
.extents.z); Vector3 topLeft
= worldBounds
.center + new Vector3
(-worldBounds
.extents.x, worldBounds
.extents.y, worldBounds
.extents.z); Vector3 bottomRight
= worldBounds
.center + new Vector3
(worldBounds
.extents.x,
-worldBounds
.extents.y, worldBounds
.extents.z); Vector3 topRight
= worldBounds
.center + new Vector3
(worldBounds
.extents.x, worldBounds
.extents.y, worldBounds
.extents.z);
//Convert each point to screen space
bottomLeft = player.MainCamera.camera.WorldToScreenPoint(bottomLeft);
topLeft = player.MainCamera.camera.WorldToScreenPoint(topLeft);
bottomRight = player.MainCamera.camera.WorldToScreenPoint(bottomRight);
topRight = player.MainCamera.camera.WorldToScreenPoint(topRight);
Vector3 center = player.MainCamera.camera.WorldToScreenPoint(worldBounds.center);
//Calculate the extends
float extendsX = Vector3.Distance(bottomLeft, bottomRight) / 2.0f;
float extendsY = Vector3.Distance(bottomLeft, topLeft) / 2.0f;
float extendsZ = 0.0001f;
Vector3 extends
= new Vector3
(extendsX, extendsY, extendsZ
);
//Return the screen bounds
return new Bounds
(center, extends
); }
The problem is that there is never a collision detected between the screen space bounds I'm generating. I'm not sure if this is the correct way to do it or if NGUI has this implemented already.
Is the screen space bounds idea/code alright? Is there something else I should do?
Any help would be greatly appreciated.
Thanks