- Home /
Question by
Fabulous-Design · Jul 25, 2014 at 05:48 AM ·
camerapixel
Pixel Unit to Scale Unit
question.jpg
(303.5 kB)
Comment
Are you using a perspective or orthographic camera? You write about the pixel size, but what are you using to display these images: mesh in world space, quad in world space, GUITexture, GUI.DrawTexture().
Answer by DajBuzi · Jul 25, 2014 at 06:27 AM
Hello,
I'll describe 2 methods here GUI and Mesh, camera size realy doesnt matter. So with the GUI you have to get the corner of the screen and divide it by 100 andmultiply by your value, Its that simple:
public float fromLeft;
public float fromRight;
public float fromTop;
public float fromBottom;
Vector2 corner = new Vector2( cam.pixelWidth, cam.pixelHeight );
fromLeft = corner.x / 100 * fromLeft;
fromRight = corner.x / 100 * fromRight;
fromTop = corner.y / 100 * fromTop;
fromBottom = corner.y / 100 * fromBottom;
Thats all about GUI now for the Mesh part it's almost the same but here you have to "convert" screen position ( x, y ) to world position( x, y, z ):
//using the same variables
Vector3 corner = cam.ScreenToWorldPoint(new Vector3(cam.pixelWidth, cam.pixelHeight, distanceFromCamera));
fromLeft = corner.x / 100 * fromLeft;
For more information about ScreenToWorldPoint see the documentation. Hope it helps.
Regards, M.Rogalski