- Home /
How does the aspect ratio work?
I'm testing to fit my game world to fit in every screen. I want to make my game only in portrait mode and showing letter box (black bars at top and bottom) is also fine. So I googled it and found a code snippet here. So I insert in my script the following parts.
float targetaspect = 3.0f / 4.0f;
// determine the game window's current aspect ratio
float windowaspect = (float)Screen.width / (float)Screen.height;
// current viewport height should be scaled by this amount
float scaleheight = windowaspect / targetaspect;
Camera camera = GetComponent<Camera>();
Rect rect = camera.rect;
rect.height = scaleheight;
camera.rect = rect;
From the above code the game world and items inside it are show properly and are not clipped or cut off whatever the screen size is. That's what I want. But the question is how that code work? Why do I need to specify Camera Rect Height value? And why does [windowaspect/targetaspect] need? Can someone explain me or provide some learning resource about the Unity aspect ratio, camera, rect, etc?
for someone to answer this you may want to include the platform you are working with. things seem a bit different between mobile and desktop.
Your answer
Follow this Question
Related Questions
How to force the game to be stretched to fit the screen? 1 Answer
Changing the Camera viewport rect results in resizing the objects rendered by the specific camera ? 1 Answer
Changing camera aspect ratio with non-fullscreen viewport 0 Answers
Having Issues with Camera Viewport Rect 0 Answers
Ortho camera viewport setup? 3 Answers