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
1
Question by Aubrey-Falconer · Dec 06, 2009 at 05:51 PM · gameobjectscene

How to determine bounding box of scene?

My objective is to determine the total dimensions of a scene.

Will I need to write a script to iterate through each object in the scene and continue adding each to an iteratively expanded "box", or is there a more elegant method available?

Thanks,

-Aubrey

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
3
Best Answer

Answer by jonas-echterhoff · Dec 06, 2009 at 05:57 PM

I don't think there is a more elegant method, but then, that script should be pretty short:

Bounds b = new Bounds(Vector3.zero, Vector3.zero);
foreach (Renderer r in FindObjectsOfType(typeof(Renderer))
    b = b.Encapsulate(r.bounds);

If you want to do this every frame, you may consider caching the array of renderers, since FindObjectsOfType is slow, though.

Comment
Add comment · Show 3 · 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 Aubrey-Falconer · Dec 06, 2009 at 06:04 PM 0
Share

Excellent! Far more elegant than the script I was envisioning :) Thanks again!

Yes, I only plan to run this script once

avatar image Ashkan_gc · Dec 06, 2009 at 06:49 PM 0
Share

if you have objects that they have colliders without renderers, you should take care of them

avatar image HeywoodFloyd · Sep 08, 2010 at 12:09 AM 2
Share

You need to set the initial value of Bounds b based on the first Renderer. Otherwise, b.$$anonymous$$ values might stay zero if there are no negative coords. Similar for b.max if there aren't positive coords.

avatar image
3

Answer by mnml_ · Oct 07, 2015 at 11:14 AM

 var rnds = FindObjectsOfType<Renderer>();
 if (rnds.Length == 0)
    return; // nothing to see here, go on
 
 var b = rnds[0].bounds;
 for (int i = 1; i < rnds.Length; i++)
    b.Encapsulate(rnds[i].bounds);
 
 // now b holds the bounds of the scene
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 AubryStrawberry · Jan 29, 2017 at 01:48 PM 0
Share

This is the correct answer, as it accounts for the $$anonymous$$ bounds properly.

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

2 People are following this question.

avatar image avatar image

Related Questions

How Do You Have Multiple High Scores For 1 GameOverScene? 1 Answer

Enable/Disable objects, with script 9 Answers

Trouble while trying to manipulate object. 2 Answers

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

Retaining GameObject data when switching scenes? 4 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