- Home /
[UNSOLVED] Get a GameObject width and height in pixels (JS)
I'm trying to put a button directly on a 2d GameObject, and I need to get the pixel width and height of the object that is underneath to properly size the button. Is there any way to get that?
Thanks!
Answer by Eric5h5 · Oct 05, 2014 at 11:53 PM
Size in Unity is measured with units, not pixels. You can use Mesh.bounds to get the bounds of an object's mesh.
Sorry, trying to modify the button. It's incorrectly sized.
Also, how would I convert it to an x and y coords on the screen for a button?
Answer by robertbu · Oct 06, 2014 at 01:24 AM
Perspective or Orthographic camera? Can we assume anything about the nature or rotation of the game objects?
In general you can get something close by converting a bounds into screen points and then finding the maximum and minimum X and Y values.
Use either the corners of the renderer.bounds of the object or use the mesh.bounds and then convert the eight corners from local to world coordinates.
For each of the eight corners, do a WorldToScreenPoint().
Cycle through the resulting positions and find the minimum/maximum x and y
Subtract min from max to get the width and height
Here is an answer that with just a bit of modification will give you those values. 'width' and 'height' on lines 43 and 44 are what you are looking for:
http://answers.unity3d.com/questions/691066/how-to-calculate-screen-area-coverage-of-a-3d-obje.html
Note for particular setups, you may be able to do the calculations with less work.
There were a couple of things I did not like about the code I pointed you at, so here is the function based on the link:
#pragma strict
function Update() {
var size = CalcScreenSize(gameObject);
Debug.Log("Object screen size = " + size);
}
static function CalcScreenSize(go : GameObject) : Vector2 {
var $$anonymous$$X = $$anonymous$$athf.Infinity;
var $$anonymous$$Y = $$anonymous$$athf.Infinity;
var maxX = -$$anonymous$$athf.Infinity;
var maxY = -$$anonymous$$athf.Infinity;
var mf = go.GetComponent.<$$anonymous$$eshFilter>();
if (mf == null) return Vector3.zero;
var t = go.transform;
var bounds = mf.mesh.bounds;
var v3Center = bounds.center;
var v3Extents = bounds.extents;
var corners : Vector3[] = new Vector3[8];
corners[0] = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top left corner
corners[1] = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top right corner
corners[2] = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom left corner
corners[3] = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom right corner
corners[4] = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top left corner
corners[5] = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top right corner
corners[6] = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom left corner
corners[7] = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom right corner
for (var i = 0; i < corners.Length; i++) {
var corner = t.TransformPoint(corners[i]);
corner = Camera.main.WorldToScreenPoint(corner);
if (corner.x > maxX) maxX = corner.x;
if (corner.x < $$anonymous$$X) $$anonymous$$X = corner.x;
if (corner.y > maxY) maxY = corner.y;
if (corner.y < $$anonymous$$Y) $$anonymous$$Y = corner.y;
}
return Vector2(maxX - $$anonymous$$X, maxY - $$anonymous$$Y);
}
Answer by jmparavicini · Oct 06, 2014 at 12:21 AM
Try this Maybe it wil work and maybe i forgot something
#pragma strict
var gameObject1 : Transform;
var gameObject2 : Transform;
var gO1X : float;
var gO1Y : float;
var gO1Z : float;
function Update()
{
gO1X = gameObject1.transform.position.x;
gO1Y = gameObject1.transform.position.y;
gO1Z = gameObject1.transform.position.z;
if(Input.GetKeyDown(KeyCode.F))
{
gameObject2.transform.position.x = gO1X;
gameObject2.transform.position.y = gO1Y;
gameObject2.transform.position.z = gO1Z;
}
}
in the gameObject1 put the transform of your first gameobject and on the gameObject2 the transform of your second gameObject
cheers skullbeats1
You're just setting the position of one object to the position of another, so it doesn't answer the question, but if you were going to do that anyway, 90% of that code is unnecessary and should be removed. There's no reason to use 3 separate floats when Vector3 exists.
var gameObject1 : Transform;
var gameObject2 : Transform;
function Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F)) {
gameObject2.position = gameObject1.position;
}
}
I'm trying to put an object over a button, my apologies. Bad wording. I need to find the width and height of a gameobject so I can put the accurate button size
Your answer
Follow this Question
Related Questions
JS file deleting multiple game objects 0 Answers
Get game component type? 1 Answer
JavaScript 3 Arrays questions 1 Answer
Track debug - Object dissapears after collision 0 Answers
Add force to GameObject 2 Answers