Skip to content

Instantly share code, notes, and snippets.

@HilariousCow
Created February 24, 2015 10:19
Show Gist options
  • Save HilariousCow/da6e8e89d761c0848e63 to your computer and use it in GitHub Desktop.
Save HilariousCow/da6e8e89d761c0848e63 to your computer and use it in GitHub Desktop.
A few helpful extensions to get the render bounds (world space) of a game object, including all its children. Handy for placement at the edges of groups of meshes/text, or finding dead center.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class BoundsHelperExtensions
{
public static Bounds WorldBounds<T>(this List<T> listOfThings) where T : MonoBehaviour
{
Bounds bounds = new Bounds(listOfThings[0].transform.position, Vector3.zero);
foreach (T thing in listOfThings)
{
bounds.Encapsulate(thing.transform.position);
}
return bounds;
}
public static Bounds RenderBounds<T>(this List<T> listOfThings) where T : Renderer
{
Bounds bounds = listOfThings[0].bounds;
foreach (T thing in listOfThings)
{
bounds.Encapsulate(thing.bounds);
}
return bounds;
}
public static Bounds RenderBounds(this Transform transform)
{
List<Renderer> rends = new List<Renderer>(transform.GetComponentsInChildren<Renderer>());
if (rends.Count > 0)
{
return rends.RenderBounds();
}
else
{
return new Bounds(transform.position, Vector3.zero);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment