Search Unity

Object's size in game units

Discussion in 'Scripting' started by Charles Hinshaw, Mar 28, 2008.

  1. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    I've been eyeballing objects against a 1x1x1 cube, but needed to get the size of an object in game units. is this the best way to do this, accounting for rotations. It seems to be working:


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. public class GetSize : ScriptableObject {
    7.     private static Quaternion rotation;
    8.     private static Vector3 size;
    9.     private static GameObject go;
    10.     [MenuItem ("GameObject/Get Renderer Size")]
    11.     static void MenuGetSize(){
    12.         rotation = Selection.activeTransform.localRotation;
    13.         Selection.activeTransform.localRotation = Quaternion.identity;
    14.         if (Selection.activeTransform.gameObject.renderer != null){
    15.             size = Selection.activeTransform.gameObject.renderer.bounds.size;
    16.             Selection.activeTransform.localRotation = rotation;
    17.             EditorUtility.DisplayDialog("Object Scale", "X: "+size.x+", Y: "+size.y+", Z: "+size.z, "OK", "");
    18.         } else {
    19.             EditorUtility.DisplayDialog("Oops", "There is no renderer available on this object.", "OK", "");   
    20.         }
    21.     }
    22. }
    23.  
    24.