Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by pretender · Oct 11, 2010 at 12:41 PM · renderboundswireframeboundingbox

render the bounding box of collider or mesh how to?

i want to figure out how to draw the bounding box of the mesh or collider that is attached to the mesh. i dont know what to use, bounding box of the mesh, collider or renderer? my goal is to get the position of all vertices making the bounding box and then render the lines between so that i can get wireframe cube, this way it will be easy to determine what object is selected in the scene.

is there a easy way to do this? and how to get the vertices. i can get the center of the bounding box like this:

Debug.Log(transformTemp.collider.bounds.center.ToString());

thanks!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by QuantumRyan · Jun 29, 2011 at 09:09 PM

I do something like this in a script attached to the GameObject. It allows me to use the in scene Gizmo's toggle, as well as my own selection options (like mouse click). (This only applies in the unity Game and Scene windows FYI.)

   public bool _bIsSelected = true;
 
   void OnDrawGizmos()
   {
     if (_bIsSelected)
       OnDrawGizmosSelected();
   }
 
 
   void OnDrawGizmosSelected()
   {
     Gizmos.color = Color.yellow;
     Gizmos.DrawSphere(transform.position, 0.1f);  //center sphere
     if (transform.renderer != null)
       Gizmos.DrawWireCube(transform.position, transform.renderer.bounds.size);
   }
Comment
Add comment · Show 1 · Share
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 HiddenMonk · Feb 26, 2015 at 11:16 PM 0
Share

The above script works fine, but I noticed more accurate results when ins$$anonymous$$d of using transform.position in "Gizmos.DrawWireCube(transform.position, transform.renderer.bounds.size);" I use transform.renderer.bounds.center

The documentation even says renderer.bounds.center is more precise http://docs.unity3d.com/ScriptReference/Renderer-bounds.html

edit - although using renderer.bounds.center do accurately show the bounding box covering all of the mesh, When doing calculations in regard to the collider, then using the renderer wont be accurate. So Ins$$anonymous$$d use collider.bounds.center and collider.bounds.size

(side note - I don't really like how every little edit I do bumps the thread in unity answers)

avatar image
1

Answer by ga5p0d3 · Nov 19, 2019 at 10:05 AM

Here's my version, which ensures all (self and) child meshes have their bounds drawn and the full object/sub-object transform is used to draw each bounding box.

 void OnDrawGizmosSelected()
 {
     Gizmos.color = Color.magenta;
     foreach(var mf in GetComponentsInChildren<MeshFilter>())
     {
         Gizmos.matrix = mf.transform.localToWorldMatrix;
         Mesh m = mf.sharedMesh;
         Gizmos.DrawWireCube( m.bounds.center, m.bounds.size );
     }
 }





Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Skinned Mesh disappears if the camera is too close. 0 Answers

Instantiate position tracking? 1 Answer

"Recalculate" the mesh bounds to the objects rotation? 3 Answers

Reporting the vertices of a 3D Bounding Box to Screen position 0 Answers

Why isn't Bounds.Encapsulate working as expected? 1 Answer


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