0
\$\begingroup\$

I am trying to make progress in Unity on understanding how aspect ratios and resolution work. I can't quite wrap my head around the topic. For example, I am trying to make a simple breakout style game, but in order to do that, I need to consider the world units and how each brick would fit into the scene. From what I have read, world units are dependent on screen resolution and aspect ratio. How would I be able to make something like this if mobile games are played on different devices (and ones with notches nonetheless). Also I'd like to know how to achieve this without using the tacky black bars that some games use.

Any information regarding this would be extremely appreciated.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ "world units are dependent on screen resolution and aspect ratio" isn't really true. Your world units stay exactly the same, on every device. Your view into that world is determined by your device's screen. So depending on your needs, you can choose to zoom out and show more of your world, or zoom in to show less, bigger, depending on your device. So, your first step is to decide what you want your game to look like on your target resolutions/devices. Mock up a screenshot in a few common aspect ratios - then we can help you find ways to achieve those outputs from Unity. \$\endgroup\$ – DMGregory Nov 2 '20 at 23:19
0
\$\begingroup\$

If your project is designed correctly, you should never need to adjust world units or positions of most objects for different screen resolutions/aspect ratios. You would instead adjust the camera.

To accommodate different aspect ratios, you would write a script that adjusts the camera zoom or position based on the screen aspect ratio. For example,

//This is a very simple example. You will need to adjust the numbers for 
//your specific game and may need to add more if-else conditions or make
//other changes (e.g. adjust the camera position instead of the FOV)
[RequireComponent(typeof(Camera))]
public class CameraAspectRatioAdjuster : MonoBehaviour {
    void Start() {
        Camera camera = GetComponent<Camera>();
        float aspectRatio = Screen.width / Screen.height;
        //you will need to change these numbers
        if (aspectRatio > (16 / 9f) {
            camera.fieldOfView = 60;
        } else if (aspectRatio > 4 / 3f) {
            camera.fieldOfView = 55;
        } else {
            camera.fieldOfView = 50;
        }
        //alternatively, you could try a one-size-fits-all-formula
        //camera.fieldOfView = aspectRatio * 60;
    }
}

There is no need for this script to account for resolution.

As for the user interface / HUD, that should be built in a Canvas using a canvas scaler. A properly built canvas with canvas scaler will generally look good on different screen resolutions/aspect ratios without needing to make any layout adjustments from code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.