Runtime bounding boxes in Unity
A Runtime bounding boxes example
The Unity has its own bounding boxes buit in editor which show up when you manipulate the objects, like transform boxes and collider boxes, but those are unavailable at runtime.
This project can help you to draw the bounding boxes ingame, using GL.Lines. The boxes get aligned to the local space of the GameObjects where they are attached.
The points calculation is taken from the renderers.bounds from all renderers in the GameObject hierarchy.
Alternatively if you wanted some other size – than calculated from the renderers bounds – for your bounding box, the script contains a functionality to use the box collider instead. You can attach and size a box collider to the GameObject, check the “colliderbased” variable on the script in the inspector – and the calculation will follow the box collider.
Obviously in order to get a mouse-click interaction on the objects – like in the demo scene – there have to be colliders attached to GameObject.
If the parent GameObject itself has no collider and the colliders are attached to the child objects only, it is necessary to attach a rigidbody to the parent GameObject in order to pass on the mouse-click collider interaction to parent.
The below screenshot links to the webplayer demo.
You can download the complete package, containing the demo scene from the picture above from here:
Some of the models used in the scene come from here: http://www.3dmodelfree.com
A version with simpler scene – if you prefer not to clutter your disk too much – is available on the AssetStoreas the BoundBoxes package.
A bit advanced version of this package is also available at the Unity AssetStore as the DimBoxes package. That will also let you place the dimensions and extension lines, here is the demo:
The demo scene in the DimBoxes package is a bit simplified, but the scripts used are the same. All the above should work fine both in Unity Indie as well as Unity Pro.
You can also have a look at the 3d object viewer that I created with that in the link below.:
This however uses the Unity Pro feature of dynamic loading of the streamed scenes. The WebPlayer itself has 150 kB or even less of data only and subsequent scenes are loaded/unloaded into it.
15 Comments
Leave a Reply
You must be logged in to post a comment.
Hi!
Thank you on sharing your amazing solution. I was trying to use it but, it’s not showing numbers on bounds?
Do you know why?
Thanks!
It is true, the numbers are not included in the download at the moment. Just let me know if you need them and I can send.
Hello!
I need the numbers too, and am very grateful if you could send them to me.
Thanks
well done my friend,
can you teach us how to add numbers too?
thanks
This should become available on the Asset Store – probably at 5 eur – I have just resubmitted the package after corrections yesterday. I hope I will not get declined this time, but keep your fingers crossed. I will let you know when it gets there, anyway.
Really Nice for your sharing this Nice Effect. I downloaded the files and using Unity 4.1. I’m getting these errors. Don’t know if I need to setup anything special for it to work?
RenderBounds.js(63,20): BCE0019: ‘alpha’ is not a member of ‘Object’.
RenderBounds.js(64,25): BCE0019: ‘Corners’ is not a member of ‘Object’.
CalculateBounds.js(93,20): BCE0019: ‘drawBox’ is not a member of ‘Object’.
I think a time has come for me to rewrite that to C# and include a demo scene into the download. Hopefully I should make that in the coming weekend.
Hey, I have just updated the download. Now that is a complete package containing the demo scene. Another, commercial package, contains dimension boxes and is in the submission to AssetStore.
Now that’s sulbet! Great to hear from you.
There’s a lot of wealthy families in China and for sure, most of them would be purchasing LTCI coverage for their loved ones. While the industry is gearing up to target this market, hopefully, there would also be plans that can be more affordable. A big portion of the population is not ultra rich but those who would need future health care would also come from them. Hopefully, those who would check out would not easily be discouraged to purchase a plan because of skyrocketing costs.
Hi! I’ve just found your solution for drawing bounding boxes at runtime. I tried to write my own, but stuck at rotation and scaling dependencies. So, I would like to see your solution. But when I import your package, Unity fails with fatal error with text “type==kMetaAssetType & pathName.find (“library/metadata”)!=0″
So, please do something with original package for me to import it correctly.
And please, give the link to your commercial package on Asset Store, if it confirmed already.
Thanks!
I bought the dimboxes package, very good inversion for what I need but … there is something that I would like to ask you. The script draws the box corectly but my objects are moving and the box remains behind. Could you give me a tip how can I achieve to move it with the object. Thank you in advance
I could have anticipated that.
It is just a matter of some small changes in the code. I think I should eventually update the package, but now I have just mailed the updated script to you.
Thank you. It works like a charm!
Hi,
Awesome work. Could also send me the updated script where the box follows the position of GameObject