Runtime bounding boxes in Unity

Posted by admin on July 6, 2011 in Unity3d |

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:

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.:

3d object viewer

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.


  • Irich says:

    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?

  • Tim Nugent says:

    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’.

    • admin says:

      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.

    • admin says:

      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.

    • Millicent says:

      Now that’s sulbet! Great to hear from you.

    • http://www./ says:

      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.

  • Ilya says:

    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.

  • realshyfox says:

    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

    • admin says:

      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.

  • realshyfox says:

    Thank you. It works like a charm!

  • Viru says:

    Awesome work. Could also send me the updated script where the box follows the position of GameObject

Leave a Reply

Copyright © 2011-2024 virtualPlayground All rights reserved.
VP based on Desk Mess Mirrored v1.8.3 theme from