- Home /
How do I keep everything inside of the screen on a laptop?
So everything works fine and dandy on my desktop PC with 1920x1080. But when I try on a MacBook which is 2560x1600 or 16:10 the game gets cut off on the left and right side. The images in the game are 16:9 which fill up the whole scene and are essiential to play the game. How do I make it so its always 16:9 no matter what resolution the player picks?
For example on a 16:10 screen I would not care if there were black bars on the top and bottom of the screen, as long as all of the image in game is visible. Is there a setting in the player/build settings that can help me achieve this? Even if I allow only 16:9 and start the build the only available resolution on the mac is 1280x800, which is not even 16:9.
I hope you understand my problem. Thank you for your help.
Answer by DiegoSLTS · Apr 27, 2019 at 01:45 PM
Unity sets the aspect ratio of the camera to match your screen aspect ratio, but you can change it on runtime using: https://docs.unity3d.com/ScriptReference/Camera-aspect.html
If you set the aspect ratio to a fixed value, you'll see the same stuff in any screen you play, but the image will be stretched to fit the whole screen. If you want everything with the correct proportions you should change the "viewport" (as shown in the inspector) too using: https://docs.unity3d.com/ScriptReference/Camera-rect.html
This will result in the sides of the screen not being used by the camera. If you do all this with a black screen you'll end up with black bars at the sides like you wanted.
Another solution could be to set the aspect ratio, then render the main camera to a RenderTexture, and then using that render texture in a UI RawImage centered, with a AspectRatio constraint. This one can be setup easier in the editor, but it has some unnecessary overhead.
Also, there's another, uglier solution. You can just render the game with the default configuration and make an Overlay Canvas with some black bars at the sides, and then set their positions at runtime based on the screen aspect ratio to cover what you don't want to show to the player. Again, this has some overhead and it's an ugly solution, but would have the same result.
Answer by Eyes-Wide-Shut · Apr 25, 2019 at 02:16 AM
You should never make your game with the intention of a single aspect ratio or resolution, rather you should adjust your game to the player's system. You can change the aspect ratio of the camera with the Viewport Rectangle property. https://docs.unity3d.com/ScriptReference/Camera-rect.html
The viewport property only affects what part of the screen the camera uses, it doesn't affect the aspect ratio of the actual camera.
Also, "you should never" is not a good advice. There are games that might need a single aspect ratio for their mechanics and there are ways to make it work, so wanting a fixed aspect ratio is as valid as supporting any aspect ratio.