Object size

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.

If you mean bounding box of a mesh object then use

mesh.bouds and it will return all AABB info.

–Grinder

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.

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

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.

Sure it does:

class ShowSize extends EditorWindow {

	@MenuItem ("Window/Show Size")
	static function Init () {
		var sizeWindow = new ShowSize();
		sizeWindow.autoRepaintOnSceneChange = true; 
		sizeWindow.Show();
	}
	
	function OnGUI () {
		var thisObject = Selection.activeObject as GameObject;
		if (!thisObject) {return;}
		
		var mf : MeshFilter = thisObject.GetComponent(MeshFilter) as MeshFilter;
		if (!mf) {return;}
		
		var mesh = mf.sharedMesh;
		if (!mesh) {return;}
		
		var size = mesh.bounds.size;
		var scale = thisObject.transform.localScale;
		GUILayout.Label("Size\nX: " + size.x*scale.x + "   Y: " + size.y*scale.y + "   Z: " + size.z*scale.z);
	}
}

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

135843--4990--$picture_1_325.png

2 Likes

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.

Here is our Eric :). Problem Solve :wink:

Here’s Eric’s code in C#

using System.Collections;
using UnityEngine;
using UnityEditor;

class ShowSize : EditorWindow
{
    [MenuItem("Window/ShowSize")]
    static void Init()
    {
        // Get existing open window or if none, make a new one:
        ShowSize sizeWindow = new ShowSize();
        sizeWindow.autoRepaintOnSceneChange = true;
        sizeWindow.Show(); 
    }

    void OnGUI () {
      GameObject thisObject = Selection.activeObject as GameObject;
      if (thisObject == null) 
      { 
          return;
      }
      
      MeshFilter mf = thisObject.GetComponent(typeof(MeshFilter)) as MeshFilter;
      if (mf == null)
      {return;}
      
      Mesh mesh = mf.sharedMesh;
      if (mesh == null)       
      {return;}
      
      Vector3 size = mesh.bounds.size;
      Vector3 scale = thisObject.transform.localScale;
      GUILayout.Label("Size\nX: " + size.x*scale.x + "   Y: " + size.y*scale.y + "   Z: " + size.z*scale.z);
   } 

}

239472–8569–$showsize_735.cs (982 Bytes)

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

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).

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.

    void OnSelectionChange()
    {
        if (Selection.activeGameObject != null)
        {
            Repaint();
        }
    }

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.

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.

I couldn’t find the widget under “windows>showsize” after following the instructions by @Eric5h5. I’m on a Mac.

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” being “ShowSize” (in line 10 of the above script). And the console warning went away nicely. Here is the unity ref:

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.

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?

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.

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 :smile:

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
{
<>

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

<>

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

<>
<< SO WOULD THIS BUT IT WOULD BE UGLY AND WED HATE YOU FOREVER>>
<<HERE TOO, BUT IT IS ALSO UGLY!>
}

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 :smile: