Search Unity

Discussion Need help fixing aspect ratio manager

Discussion in 'Editor & General Support' started by joxthebest314, Mar 18, 2023.

  1. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95
    Hi, I am currently having issues with the aspect ratio of my game. I have a laptop with a 3:2 ratio and managed to make a script that will lock the aspect ratio of the game to 16:9.
    Everything works fine except when the game has a taller or larger resolution than the screen. When this happens, the black bars are still correctly rendered but the game will think he has more room than there is.
    For example with my laptop resolution of 2256x1504 and the game set at 2560x1440, the ratio calculated by the game will be wrong and canvas components will use the black bars like normal space.

    Here are pictures showing the ratio calculated when the resolution of the game is > than the screen and when screen > game.
    good.png


    bad.png

    Here is an example of black bars being used by a canvas element when game > screen (bottom bars are ignored)
    black bars not working.png

    Here is how it is when screen res > game (and how it should always be)

    black bars working.png


    I really don't understand what I did wrong and why it is so hard to lock the game at 16:9.

    Here is the script I used :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class AspectRatioManager : MonoBehaviour
    5. {
    6.     private float targetAspectRatio = 16f / 9f; // Remplacez cette valeur par l'aspect ratio souhaité
    7.     private Camera cam;
    8.     void Awake()
    9.     {
    10.         cam = GetComponent<Camera>();
    11.         UpdateRatio();
    12.     }
    13.     private void Update()
    14.     {
    15.         UpdateRatio();
    16.     }
    17.     public void UpdateRatio()
    18.     {
    19.         cam.aspect = targetAspectRatio;
    20.         float currentAspectRatio = (float)Screen.width / (float)Screen.height;
    21.         float scaleHeight = currentAspectRatio / targetAspectRatio;
    22.         float scaleWidth = targetAspectRatio / currentAspectRatio;
    23.         if (currentAspectRatio > targetAspectRatio)
    24.         {
    25.             cam.rect = new Rect((1f - scaleWidth) / 2f, 0f, scaleWidth, 1f);
    26.         }
    27.         else
    28.         {
    29.             cam.rect = new Rect(0f, (1f - scaleHeight) / 2f, 1f, scaleHeight);
    30.         }
    31.     }
    32.  
    33.  
    34.     private void OnGUI() //pour afficher des infos
    35.     {
    36.         GUILayout.BeginArea(new Rect(0, 100, 200, 500));
    37.         GUILayout.BeginVertical();
    38.         GUILayout.Label("current ratio : " + ((float)Screen.width / (float)Screen.height));
    39.         GUILayout.Label("screen.height : " + ((float)Screen.height));
    40.         GUILayout.Label("screen.width : " + ((float)Screen.width));
    41.         GUILayout.Label("currentResolution.height : " + ((float)Screen.currentResolution.height));
    42.         GUILayout.Label("currentResolution.width : " + ((float)Screen.currentResolution.width));
    43.         GUILayout.EndVertical();
    44.         GUILayout.EndArea();
    45.     }
    46. }
    47.  
    Note : I am currently using unity 2022.2.1f1
     

    Attached Files:

    • good.png
      good.png
      File size:
      1,001.5 KB
      Views:
      70