- Home /
Hi every one, , not how to rescale an object.
i need to know how to obtain the size of an object in pixels, something like gameobject cube 234x123 pixels may be it is easy but i dont know how to do it, i need a script to get the size in pixels of a cube or a botton preferably, and show it in the unity console, it is because i need to know the measures of the textures i need to put in the box or button so that the texture looks good and not to pixeled. i found here some code like this:
[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);
}
but this just give me the scale of the object in a show size windows, and that doesn't work for me. thanks a lot by the way, im new in unity3d edition =)
Answer by robertbu · Oct 11, 2014 at 03:54 AM
The pixel size of an object with a perspective camera varies based on the distance from the camera. In addition, as you change the resolution of the app (as would happen when moving from one mobile device to another), the pixels also change. Typically if you have a specific device and are looking for pixel perfect, you do it in reverse...you pick a pixel size for the texture then setup the environment to display it at or near pixel perfect. If you really want to know the pixel size of an object there is some code in this answer:
http://answers.unity3d.com/questions/651090/get-width-of-2d-game-object.html
Note this calculates the bounds of the mesh of the object, so the results will depend on the rotation of the object. So for example, if you wanted the pixel size for the bottom of the cube, the bottom would need to be axes aligned and facing an axes aligned camera.
Your answer
Follow this Question
Related Questions
Unity 5.4.1p4 No longer outputting Asset Sizes after build? 0 Answers
Rect Transforms not scaling the same? 1 Answer
About how many pixels does the average user need before they can efficiently see and tap an object? 1 Answer
How I get sizeof pixels of object 1 Answer
Question About Terrain 1 Answer