• 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
4
Question by jhooks098 · Oct 15, 2012 at 08:43 PM · prefabbounds

Getting bounds of a prefab with multiple children

I have a bunch of prefabs as children of an empty game object, is there an easy way of determining what the total bounds of that grouped prefab is?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by lassade · Feb 08, 2014 at 01:49 AM

i wrote script that may help.

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEditMode]
 public class PrefabAABB : MonoBehaviour
 {
     /// <summary>
     /// local space Axis Aligned Bounding Box
     /// </summary>
     public Bounds bounds;
     Transform _transform;
 
     void OnDrawGizmos()
     {
         Gizmos.DrawWireCube(transform.position + bounds.center, bounds.size);
     }
 
     void Reset ()
     {
         RecalculateBounds();
     }
 
     public Bounds WorldBounds ()
     {
         if (_transform == null)
             _transform = transform;
 
         Bounds b = bounds;
         b.center += _transform.position;
 
         Vector3 size = b.size;
         Vector3 tsize = _transform.lossyScale;
         size.x *= tsize.x;
         size.y *= tsize.y;
         size.z *= tsize.z;
         b.size = size;
 
         return b;
     }
 
     [ContextMenu("Recalculate Bounds")]
     public void RecalculateBounds ()
     {
         MeshFilter this_mf = GetComponent<MeshFilter>();
         if (this_mf == null)
         {
             bounds = new Bounds(Vector3.zero, Vector3.zero);
         }
         else
         {
             bounds = this_mf.sharedMesh.bounds;
         }
 
         MeshFilter[] mfs = GetComponentsInChildren<MeshFilter>();
         foreach (MeshFilter mf in mfs)
         {
             Vector3 pos = mf.transform.localPosition;
             Bounds child_bounds = mf.sharedMesh.bounds;
             child_bounds.center += pos;
             bounds.Encapsulate(child_bounds);
         }
     }
 
 #if UNITY_EDITOR
     void Update()
     {
         if (Application.isPlaying) return;
         RecalculateBounds();
     }
 #endif
 }
 
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
avatar image
5

Answer by flberger · Jun 12, 2013 at 01:30 PM

You can construct an enclosing bounding box by instanciating Bounds and then repeatedly call Bounds.Encapsulate(bounds : Bounds) for all children to integrate their bounding boxes. See the links for details.

Comment
Add comment · Show 2 · 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 g8minhquan · Mar 07, 2018 at 06:57 AM 0
Share

Here's the code to do what flberger said:

 public Bounds bounds;
 public void CalculateBounds ()
 {
     bounds.size = Vector3.zero; // reset
     Collider2D[] colliders = GetComponentsInChildren<Collider2D>();
     foreach (Collider2D col in colliders)
     {
         bounds.Encapsulate(col.bounds);
     }
 }

avatar image mrbrable · Mar 30, 2019 at 09:10 PM 0
Share

Thanks mate, you just saved us from hours of problem solving once again. You're a legend!

avatar image
0

Answer by MountDoomTeam · Oct 15, 2012 at 09:21 PM

bounds as a square or as sphere?

search gameobjects in prefab, compare them all to find the one most at right, left, up down, fwds and backwards, that will give you a bounding box, or compare distance of all of them between each other, and keep the largest distance, use arrays.

Comment
Add comment · Show 2 · 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 jhooks098 · Oct 15, 2012 at 09:57 PM 0
Share

I wanted to do it with square bounds, so your saying I can just compare the bounding boxed of all children to find the greatest extends? Is the bounds property aabb?

avatar image MountDoomTeam · Oct 15, 2012 at 10:39 PM 0
Share

i would put all the children objects into an array, and then find the one with the lowest and highest z, for example sort array vector3.z by value from low to high and choose number 1 and the last one.

do the same for x and y and you will have a bounding box parralel with x y and z,

then you can take a gameobject, put it in the middle of x y and z that you found, and resize it to the difference between -z and z and -x and x etc.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I determine an object of prefab's size (for positioning when instantiating them) 2 Answers

Collider.bounds position is wrong? 3 Answers

Array of prefabs with different sizes stick to each other 0 Answers

Size of a prefab instance is always (0,0,0) 1 Answer

Deleting a Prefab when it is dragged to a GUI Texture 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