x


Get bounds.size of a game object in an LODGroup?

Hello,

How can I get the bounds size of a game object in an lod group? I tried modifying code that I found here: link text

Snippet of my C# script:

    //Code to assign m_wall the appropriate prefab is done elsewhere

    Vector3 size = Vector3.zero;
    LODGroup lodGroup = m_wall.GetComponent< LODGroup >();
    if( lodGroup )
    {
        SerializedObject obj = new SerializedObject( lodGroup );
        SerializedProperty prop = obj.FindProperty( "m_LODs.Array.data[0].renderers" );
        SerializedProperty propRenderer = prop.GetArrayElementAtIndex( 0 ).FindPropertyRelative( "renderer" );
        size = propRenderer.boundsValue.size;  // <--error message points to this line
    }
    else
    {
        size = m_wall.renderer.bounds.size;
    }
    m_wallWidth = size.x;
    m_wallHeight = size.y;
    m_wallLength = size.z;

but it doesn't work. I get the following error at runtime:

Mismatched types in GetValue - return value is junk
UnityEditor.SerializedProperty:get_boundsValue()

Am I allowed to use stuff from the UnityEditor namespace from a game script? In my game, I build levels on the fly using different prefabs. Some are simple walls, and others are complex and have an lod group. The simple walls work fine. AFAIK, I only need to get the bounds of the first lod level as the lesser levels have meshes that are the same dimensions. Can someone please tell me how to do this? Thanks.

more ▼

asked May 31, 2012 at 11:19 PM

jdwieber gravatar image

jdwieber
53 10 8 9

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Ok, so I found a simple way to do this. In my case, the object m_wall is a prefab in a script set through the inspector, so I need to instantiate a dummy object first. Then to get the bounds I get a renderer from one of the children by calling GetComponentInChildren and pull the bounds from the renderer. Here is a code snippet from my game:

    GameObject dummy = (GameObject)Instantiate( m_wall, Vector3.zero, Quaternion.identity );
    Renderer renderer = dummy.GetComponentInChildren< Renderer >();
    if( renderer != null )
    {
        Vector3 size = renderer.bounds.size;
        m_wallWidth = size.x;
        m_wallHeight = size.y;
        m_wallLength = size.z;
        Destroy( dummy );
    }
    else
    {
        Destroy( dummy );
        throw new System.Exception( "Failed to get renderer from wall object" );
    }

EDIT: For sake of completeness, the above only works for a single object. I have other prefabs that I build by combining multiple prefabs together under a single parent. In that case, things go horribly wrong. When I use those models I have to manually calculate the bounds and so the code for that looks like this:

    GameObject dummy = (GameObject)Instantiate( m_wall, Vector3.zero, Quaternion.identity );
    Renderer[] renderers = dummy.GetComponentsInChildren< Renderer >();
    if( renderers.Length >= 1 )
    {
        Debug.Log( "Found " + renderers.Length + " renderers" );
        Bounds bounds = new Bounds( Vector3.zero, Vector3.zero );
        foreach( Renderer r in renderers )
        {
            bounds.Encapsulate( r.bounds );
        }

        Vector3 size = bounds.size;
        m_wallWidth = size.x;
        m_wallHeight = size.y;
        m_wallLength = size.z;
        Destroy( dummy );
    }
    else
    {
        Destroy( dummy );
        throw new System.Exception( "Failed to get renderers from wall object" );
    }
more ▼

answered Jun 11, 2012 at 06:01 PM

jdwieber gravatar image

jdwieber
53 10 8 9

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x454
x163
x12

asked: May 31, 2012 at 11:19 PM

Seen: 2483 times

Last Updated: Jun 11, 2012 at 06:17 PM