Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

object size

Discussion in 'iOS and tvOS' started by creat327, Mar 28, 2009.

  1. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    is there an easy way to get an object size?
    I see position, scaling and so on, but what's the size of the object? I always have to grab the calculator and start multiplying numbers by the scale factor.
     
  2. 3dgrinder

    3dgrinder

    Joined:
    Oct 21, 2008
    Posts:
    249
    If you mean bounding box of a mesh object then use

    mesh.bouds and it will return all AABB info.

    --Grinder
     
  3. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    but there is no way to see it on Unity designer? It's something pretty standard in any design package. I don't want to have to program and print info to figure out the size of objects.
     
  4. 3dgrinder

    3dgrinder

    Joined:
    Oct 21, 2008
    Posts:
    249
    Well, you can see the Wireframe in the scene view. If you want the bound value in visual then tell your programmer I want to see it ;). I think it's a 10 min Job to do it.

    --Grinder
     
  5. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    Hmmm not sure I'm explaining myself correctly.
    I just want to know, when I scale an object, what's the size of it.

    For instance, I create a cube, i shrink it to 0.5 scale, I know the size now it's 0.5x0.5x0.5, that's easy because it was originally a 1. When I import objects, it is not as obvious. A scale of 0.5 doesn't mean the object measures 0.5 since I don't know the original size.

    I can go an programmatically display the bound size but that won't help me at all in the designer. It would be a nice feature to have next to the transform information in the GUI: scale, rotation and size. When you modified the 'size' it would calculate the scaling for you.

    It helps because that way I can resize the other objects in the scene to match each other. Knowing the scale doesn't help when I want several different meshes to have the same size in the designer/world.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Sure it does:

    Code (csharp):
    1. class ShowSize extends EditorWindow {
    2.  
    3.     @MenuItem ("Window/Show Size")
    4.     static function Init () {
    5.         var sizeWindow = new ShowSize();
    6.         sizeWindow.autoRepaintOnSceneChange = true;
    7.         sizeWindow.Show();
    8.     }
    9.    
    10.     function OnGUI () {
    11.         var thisObject = Selection.activeObject as GameObject;
    12.         if (!thisObject) {return;}
    13.        
    14.         var mf : MeshFilter = thisObject.GetComponent(MeshFilter) as MeshFilter;
    15.         if (!mf) {return;}
    16.        
    17.         var mesh = mf.sharedMesh;
    18.         if (!mesh) {return;}
    19.        
    20.         var size = mesh.bounds.size;
    21.         var scale = thisObject.transform.localScale;
    22.         GUILayout.Label("Size\nX: " + size.x*scale.x + "   Y: " + size.y*scale.y + "   Z: " + size.z*scale.z);
    23.     }
    24. }
    Call that ShowSize, put that in your Editor folder (make one if you don't have one), then do Window -> Show Size, and then dock it as you like. That's just a quick hack so of course you could make it prettier and whatnot. Only thing is, it doesn't seem to update when selecting different objects unless you move the mouse over the Show Size window...not sure how to fix that since I'm not really that familiar with editor scripting and the docs are a little on the light side in this area.

    --Eric
     

    Attached Files:

    Deleted User and DMorock like this.
  7. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    Nice trick!

    I wonder whether Unity would just add it to the next release. I think it's very useful to be able to resize by size not by scale.
     
  8. 3dgrinder

    3dgrinder

    Joined:
    Oct 21, 2008
    Posts:
    249
    Here is our Eric :). Problem Solve ;)
     
  9. Abdullah.Ahmed

    Abdullah.Ahmed

    Joined:
    Sep 22, 2009
    Posts:
    18
    Here's Eric's code in C#

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. class ShowSize : EditorWindow
    7. {
    8.     [MenuItem("Window/ShowSize")]
    9.     static void Init()
    10.     {
    11.         // Get existing open window or if none, make a new one:
    12.         ShowSize sizeWindow = new ShowSize();
    13.         sizeWindow.autoRepaintOnSceneChange = true;
    14.         sizeWindow.Show();
    15.     }
    16.  
    17.     void OnGUI () {
    18.       GameObject thisObject = Selection.activeObject as GameObject;
    19.       if (thisObject == null)
    20.       {
    21.           return;
    22.       }
    23.      
    24.       MeshFilter mf = thisObject.GetComponent(typeof(MeshFilter)) as MeshFilter;
    25.       if (mf == null)
    26.       {return;}
    27.      
    28.       Mesh mesh = mf.sharedMesh;
    29.       if (mesh == null)      
    30.       {return;}
    31.      
    32.       Vector3 size = mesh.bounds.size;
    33.       Vector3 scale = thisObject.transform.localScale;
    34.       GUILayout.Label("Size\nX: " + size.x*scale.x + "   Y: " + size.y*scale.y + "   Z: " + size.z*scale.z);
    35.    }
    36.  
    37. }
    38.  
     

    Attached Files:

  10. Nithel

    Nithel

    Joined:
    Feb 22, 2010
    Posts:
    9
    I can't make the script work, either of them. They both say that "An instance of ShowObject couldn't be created, because there is not script with that name." (when I click the menu item), and then refers to the script I placed in the Editor folder... Any suggestions? I'm running Unity 2.6
     
  11. danilonishimura

    danilonishimura

    Joined:
    Jul 13, 2010
    Posts:
    70
    If you're using the C# script, you should rename your script to the same name of your main class(in this case, rename the file to "ShowSize", without quotes).
     
  12. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    I haven't tested this, on my laptop, not at my workstation, so I don't have Unity installed, but this should work, or something very similar, to refresh the size when the selection changes. Let me know if it works or not.


    Code (csharp):
    1.     void OnSelectionChange()
    2.     {
    3.         if (Selection.activeGameObject != null)
    4.         {
    5.             Repaint();
    6.         }
    7.     }
    8.  
     
  13. johot

    johot

    Joined:
    Apr 11, 2011
    Posts:
    201
    I found this script extremely helpful, works great especially after adding the repaint() part by JustinLloyd.

    Shouldn't we add this to the Unity community wiki?

    Strange this doesn't exist in the editor already? Unless it's added but I can't find it.
     
  14. pn99

    pn99

    Joined:
    Feb 9, 2012
    Posts:
    20
    Exactly my sentiment. I was dumb struck to find that 'scale' in inspector is merely a relative number to the value of 1. This really is a basic measurement tool that any good tool should have, can't believe the thread was this deep with programming required to for this.
     
  15. pn99

    pn99

    Joined:
    Feb 9, 2012
    Posts:
    20
    I couldn't find the widget under "windows>showsize" after following the instructions by @Eric5h5. I'm on a Mac.
     
  16. pn99

    pn99

    Joined:
    Feb 9, 2012
    Posts:
    20
    Never mind. I got it working.
    You have to create the "Editor" folder under the "Assets" folder in order for it to show up under "Window" of unity.

    I would add something that I noticed. I received a console warning something to the effect of "cannot use new ShowSize, rather use ScriptableObject" as in ScriptableObject.CreateInstance.<MyWindow>(); "MyWindow" being "ShowSize" (in line 10 of the above script). And the console warning went away nicely. Here is the unity ref:

    http://unity3d.com/support/documentation/ScriptReference/EditorWindow.html

    fyi...

    minor note to the newbie like myself. To dock this new window, just drag the tab of the new window onto the dockable area of your choice. Phrasing the correct question for google was way tricky until a friend just showed me.
     
    Last edited: Mar 28, 2012
  17. memetic-arts

    memetic-arts

    Joined:
    Feb 27, 2011
    Posts:
    82
    Ditto! Only I can't seem to figure out how to get the repaint function to work correctly . . . I'm apparently not inserting it in the right place in the script, so am getting a compile error ("expecting }")

    Any enlightenment would be appreciated, thanks!

    And apparently, the request for integration of this function into subsequent versions has gone unheard, as it is now three years later . . . I think this should be bumped up, no?
     
  18. andresp

    andresp

    Joined:
    Aug 12, 2011
    Posts:
    38
    but what about if you want to know the size of a group of meshes (a parent with some childs, which is the common case when importing models into the scene)? have in mind that the childs often have different rotations than the parent node.
     
  19. GamingImperatrix

    GamingImperatrix

    Joined:
    Apr 30, 2012
    Posts:
    2
    Oh goodness gracious *Facepalms* Okay, I don't want to be /too/ mean, but if you're stopped dead in your tracks by an expecting } error, then go spend a little more time with a (any) programming book :D

    If something is actually wrong with the code (!) then you need to give more information about your bug, because this bug is very generic and can occur anywhere for any number of reasons.

    But odds are you've just accidentally deleted a bracket or something. Rule of thumb: Every { needs a } somewhere in the code, and they ought to 'match' (if you're quickly debugging a script someone else wrote, that means that there should be one flanking the top and bottom of every indented block/paragraph of code)

    Last but not least, you aren't sure where to put it in your script? It belongs inside the block of code owned by the 'class' statement/line-of-code. So, after the line of code that starts with 'class,' there should be a bracket. Everything past that point that's indented, all the way up to the very last reverse bracket }, the bracket that's back on your 'class' line's original indentation..... belongs to class.

    The 'repaint' snippet belongs in there. No deeper into the indentation scheme , and no shallower. Past the first bracket, Before the last bracket, and trying not to interrupt the bond between any other lines of code, and their respective blocks {}.

    class blah blah blah
    {
    <<HERE>>​

    private function other code doing its thing​
    <<NOT HERE>>​
    {​
    blah blah blah​
    }​

    <<HERE IS GOOD TOO>>​

    public function other code doing its thing​
    {​
    <<NOT HERE>>​
    <<NOT HERE EITHER CAUSE INDENTATION IS JUST FOR LOOKS SO HUMANS CAN READ IT, THE CODE DOESNT REALLY CARE>>​
    }​

    <<HERE WOULD ALSO WORK>>​
    << SO WOULD THIS BUT IT WOULD BE UGLY AND WED HATE YOU FOREVER>>
    <<HERE TOO, BUT IT IS ALSO UGLY!>​
    }
     
    Last edited: Apr 30, 2012
  20. GamingImperatrix

    GamingImperatrix

    Joined:
    Apr 30, 2012
    Posts:
    2
    Darn it let me edit that, forum text editors are always a jerk.

    Kay it should be good now. Please Correct me anyone if I've forgotten something, I'm used to C++ not C# or Javascript, so even though basic programming is known to me, language conventions aren't always perrrfect :D
     
    Last edited: Apr 30, 2012
  21. Benoit Dufresne

    Benoit Dufresne

    Joined:
    Feb 26, 2013
    Posts:
    4
    I haven't investigated why, but it seems Eric's code yields inaccurate results sometimes. I was glad to learn this was possible though! This seems to perform a tiny bit better:

    Code (csharp):
    1.  
    2. class ShowSize extends EditorWindow {
    3.     @MenuItem ("Window/Show Size")
    4.  
    5.     static function Init () {
    6.         var sizeWindow = ScriptableObject.CreateInstance("ShowSize");
    7.         sizeWindow.autoRepaintOnSceneChange = true;
    8.         sizeWindow.Show();
    9.     }
    10.  
    11.     function OnGUI () {
    12.         var thisObject = Selection.activeObject as GameObject;
    13.         if (!thisObject) {return;}
    14.  
    15.         var renderer = thisObject.renderer;
    16.         if (!renderer) return;
    17.  
    18.         var bounds = renderer.bounds;
    19.         if (bounds == null) return;
    20.  
    21.         var size = bounds.size;
    22.         GUILayout.Label("Size\nX: " + size.x +
    23.               "   Y: " + size.y + "   Z: " + size.z);
    24.     }
    25.  
    26.     function OnSelectionChange() {
    27.         if (Selection.activeGameObject != null) {
    28.             Repaint();
    29.         }
    30.     }
    31. }
    Cheers!
     
  22. Nsomnia

    Nsomnia

    Joined:
    Sep 26, 2014
    Posts:
    27
    Old thread but this is still referenced alot. In newer version of Unity if anyone gets the annoying ...must be instantiated using the ScriptableObject.CreateInstance... warning use this code instead (C#)

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. /* ############################
    7.  * ## Show Size Editor Addon ##
    8.  * ## This script reports  ##
    9.  * ## the size of a object  ##
    10.  * ## in x,y and z axis'.  ##
    11.  * ## All work by Eric5h5  ##
    12.  * ##forum.unity3d.com/threa ##
    13.  * ## ds/object-size.19991  ##
    14.  * ## C# conversion and warn ##
    15.  * ##ing fix by Nsomnia of  ##
    16.  * ## BlenderTek.com and last##
    17.  * ## function by justinlloyd##
    18.  * ############################ */
    19.  
    20. class ShowSize : EditorWindow
    21. {
    22.   [MenuItem("Window/ShowSize")]
    23.   static void Init()
    24.   {
    25.   // Get existing open window or if none, make a new one:
    26.   //Next line replaces old statment causing a warning in Unity 4.6 used to be "ShowSize sizeWindow = new ShowSize();"
    27.   ShowSize sizeWindow = ScriptableObject.CreateInstance(typeof(ShowSize)) as ShowSize;
    28.   sizeWindow.autoRepaintOnSceneChange = true;
    29.   sizeWindow.Show();
    30.   }
    31.   void OnGUI () {
    32.   GameObject thisObject = Selection.activeObject as GameObject;
    33.   if (thisObject == null)
    34.   {
    35.   return;
    36.   }
    37.    
    38.   MeshFilter mf = thisObject.GetComponent(typeof(MeshFilter)) as MeshFilter;
    39.   if (mf == null)
    40.   {return;}
    41.    
    42.   Mesh mesh = mf.sharedMesh;
    43.   if (mesh == null)  
    44.   {return;}
    45.    
    46.   Vector3 size = mesh.bounds.size;
    47.   Vector3 scale = thisObject.transform.localScale;
    48.   GUILayout.Label("Size\nX: " + size.x*scale.x + "  Y: " + size.y*scale.y + "  Z: " + size.z*scale.z);
    49.   }
    50.   void OnSelectionChange()
    51.   {
    52.   if (Selection.activeGameObject != null)
    53.   {
    54.   Repaint();
    55.   }
    56.   }
    57. }
    58.