- Home /
Why is my orthographic camera not resizing according to resolution / aspect?
I'm trying to create a quad which resizes to match the dimensions of an orthographic camera, no matter what resolution or aspect is set within Unity's Game view panel. For fixed dimensions like 960 x 600 or 1280 x 800, this seems to work fine - however, the fixed resolution of 1024 x 786 and all other aspects (5:4, 4:3, 3:2 etc) results in the camera bounds being wider than expected.
For example, setting the fixed resolution to x1024 y786 and running the code below results in the quad having the scale x1.024f, y0.768f, z1.0f, but the camera bounds appear to be (estimating by resizing a quad to fit within cam bounds) x1.24, y0.768.
x1.024f != x1.24
What on earth is going on? The function in question follows...
private void ResizeScreenAndCamera () // Comments assume the set resolution is Standalone 1024 / 768
{
canvasCamera.orthographicSize = Screen.height * 0.5f * objectScalar;
// This is 0.384f , half of 768. This height is correct, but the camera does not get the intended width (1.024) - instead, it's x is closer to 1.24
float screenAspect = (float)Screen.width / (float)Screen.height; // If the resolution in Unity is set to 1024 x 768, this results in 1.33333 etc.
// Canvas Object is a MeshRenderer showing a Quad.
float canvasObjectX = (canvasCamera.orthographicSize * screenAspect) * 2; // 0.512 * 2 = 1.024
float canvasObjectY = canvasCamera.orthographicSize * 2; // 0.384 * 2 = 0.768
canvasObject.localScale = new Vector3(canvasObjectX, canvasObjectY, canvasObject.localScale.z); // Results in desired size, height matches cam ortho height.
RenderTexture displayRT = new RenderTexture(Screen.width, Screen.height, 24);
canvasTexture = displayRT;
Debug.Log("Desktop Screen res: " + Screen.currentResolution);
Debug.Log("Screen Width " + Screen.width + " / Screen Height " + Screen.height + " = Screen Aspect " + screenAspect);
}
There are other cameras and UGUI objects in the scene, but this problem persists when they're all turned off.
Can anybody help me? Thanks in advance...!
PS: Bonus question - can anybody explain how to add a line break in Unity Answers? I've tried following the instructions, "to add a line break simply add two spaces to where you would like the new line to be and hit enter/return", but this does not seem to be working.