- Home /
Can I get the size ( height and width) of a plane in pixels?
Answer by Rabwin · May 09, 2013 at 11:05 AM
Yes, but the math is fairly difficult (for someone like me anyway). You need to know how many pixels are being taken up by that object on the scene, which means you need to measure the screenspace of each face on the object.
I'm not going to go through the math because I don't know it, but it's probably something similar to finding the z distance to the vertexes that make up the face, calculate that distance, scale the measurement to screenspace (this is the difficult part, probably...), compare that distance to the screen's width/height (which is in pixels afaik).
Good luck....
Edit: I'm sorry but I suck at explaining math...
"Basically" you're reverse engineering how the camera works. If you can find out how that math is done, then you're probably set.
Answer by robertbu · May 09, 2013 at 11:55 AM
Assuming the plane is axis aligned and parallel to the camera, you can use Camera.WorldToScreenPoint() to translate the size. Note that the build-in plane when it is scaled to (1,1,1), is 10 units on a side.
var planeScale = 10.0;
function Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
var v3 = Vector3(planeScale * transform.localScale.x, planeScale * transform.localScale.y, transform.position.z);
v3 = Camera.main.WorldToScreenPoint(v3);
var v3Zero = Camera.main.WorldToScreenPoint(Vector3.zero);
v3 = v3 - v3Zero;
Debug.Log("Image screen size: " + v3.x + " x " + v3.y);
}
}
Note this gives you a fractional amount which I think you can use Mathf.RoundToInt() but may have to do a Mathf.CeilToInt() to get the exact size. Note you will get the same value even if the plane is only partially visible.
Answer by ocimum · Oct 05, 2015 at 02:52 PM
You can access the real planes size by the mesh bounds:
Mesh planeMesh = _plane.GetComponent<MeshFilter>().mesh;
Bounds bounds = planeMesh.bounds;
// size in pixels
float boundsX = _panel.transform.localScale.x * bounds.size.x;
float boundsY = _panel.transform.localScale.y * bounds.size.y;
float boundsZ = _panel.transform.localScale.z * bounds.size.z;
Of course, the panels factor of the panel is set to 10, but in case you would change it, your script would not be correct anymore. This way you can use it on each plane, doesn't matter if the factor has been changed!
Your answer
![](https://koobas.hobune.stream/wayback/20220613101901im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to get expanding object collide when it touches others? 1 Answer
C# How to Drag and Scale with Mouse Window 0 Answers
Particles shape scales issue after upgrading Unity to 4.2.2 0 Answers
Have a question about the scale of environments and assets in them. 1 Answer
Question about creating large scale games in Unity 0 Answers