Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by Zenotek · Feb 28, 2011 at 05:05 PM · 3d

Creating a bounding box for multiple objects using bounds

I'm working on scripting an Editor tool that automatically seeks out and adds an impromptu bounding box to groups of objects by making a default cube with renderer, encapsulating them, and turning off the cube's renderer. On the face, it seems like it should be fairly simple, but I've hit a number of problems.

I wrote this function to handle the encapsulation, it takes in two GameObjects, one being the parent object to the group of GameObjects that need bounding, and the other being a default cube that will be used for the bounding box (I created this before calling this function).

void Encap(GameObject obj, GameObject blocker) {
    Object[] children = obj.GetComponentsInChildren<Renderer>();
    foreach(Renderer child in children) {
        if(child != null){
            blocker.renderer.bounds.Encapsulate(child.bounds);
        }
    }
}

This should iterate through all the children and shape the blocker to encapsulate them all, instead it does absolutely nothing. After the script is complete, I have the same default cube I started with. I've tried a few different ways of doing this, (using colliders, using renderers, making a bounds and returning) but so far it always ends in a default cube with no changes. I put in some checks to see where it was going, and it's successfully finding each child object and running the encapsulate function, it's just not doing anything.

Any help would be appreciated, are bounds somehow broken in new versions of Unity, or am I just doing stupid?

Comment
Add comment · Show 2
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 bratushka · Jan 14, 2013 at 06:19 AM 0
Share

maybe i m getting it wrong, but Encapsulate seems to accept point (Vector3) rather than bounds

avatar image bratushka · Jan 14, 2013 at 06:23 AM 0
Share

so maybe it will work like that (did not test it, just guessiing) void Encap(GameObject obj, GameObject blocker) { Object[] children = obj.GetComponentsInChildren(); foreach(Renderer child in children) { if(child != null){ blocker.renderer.bounds.Encapsulate(child.bounds.$$anonymous$$); blocker.renderer.bounds.Encapsulate(child.bounds.max); } } }

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by joshmcewin · Mar 29, 2021 at 04:38 AM

Sorry For Bumping this, but i think this would be a Solution

  private void Start()
     {
         BoxCollider boxCollider = gameObject.AddComponent<BoxCollider>();//to show it working
         Bounds bounds = new Bounds();        
         bounds = Encap(transform, new Bounds());//recursion function, must get applyed to a Bounds
         boxCollider.center = bounds.center;//apply the center and size
         boxCollider.size = bounds.size;
     }
 Bounds Encap(Transform parent, Bounds blocker)
     {
         foreach (Transform child in parent)//gets all the children in the transform
         {
             Renderer renderer = child.GetComponent<Renderer>();
             if (renderer != null)//checks if it has a renderer
             {
                 blocker.Encapsulate(renderer.bounds);//makes blocker cover the child
             }           
             blocker = Encap(child, blocker);//calls function on child to get all objects
         }
         return blocker;
     }


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 Einxeld · Apr 11 at 09:32 PM 0
Share

Thanks, works perfectly!

avatar image
0

Answer by Jake-L · Mar 01, 2011 at 03:23 PM

Is obj==blocker? Note that GetComponentsInChildren returns components from both child objects and itself. GetComponentInChildren behaves like expected and only returns childs.

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 Zenotek · Mar 01, 2011 at 03:47 PM 0
Share

The object group (obj) and the blocker (blocker) are two separate objects, so GetComponentsInChildren wouldn't pull up the blocker, it would pull up the parent of the object group however, which is what the "child != null" check is for, since the parent has no renderer.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Finding the bounds of a grouped model 2 Answers

Move object towards mouse in full 3D space 2 Answers

How can i make my camera move in 3 dimensions? 2 Answers

stop song and play song 0 Answers


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