Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to change size of the camera depending on the aspect ratio?

Discussion in 'Scripting' started by tomang5, Sep 17, 2020.

Thread Status:
Not open for further replies.
  1. tomang5

    tomang5

    Joined:
    Feb 18, 2020
    Posts:
    63
    Hi all,

    I need a help about this algorithm. I need to change the size of the camera, which is orthographic, depending on the players' aspect ratios. I designed my game for the aspect ratio of 16:9. When someone who has the aspect ratio of 18:9 for example, I need to change the size of the camera; so that the player will see the area like a player who has the aspect ratio of 16:9 (or as close as possible).

    There are some other aspect ratios like 21:9, 4:3 (tablets) etc. The algorithm should be make the FOV like 16:9. It should increase the size if necessary (4:3 for example). A code example would be great for this.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    If it is just UI you can use the AspectRatioFitter to control the UI shape, and select Fit Inside Parent.

    For the actual camera, if you like you can black-band the extra shown area top and bottom or side to side.

    Or you can change the .rect of the camera, but then you need a second camera behind it to avoid getting dead pixels at the undrawn area.

    The orthographicSize maps (in Unity) to half the height of the screen, as seen here:

    https://docs.unity3d.com/ScriptReference/Camera-orthographicSize.html

    so by default a camera with a (0,0,1,1) cliprect will fill the screen laterally however much is necessary.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    If you just want to change the size/shape of the camera so that it fills the available screen, I'm reasonably sure Unity already does that by default.

    Are you trying to get letter-boxing, where it shrinks down the 16:9 image to fit inside of whatever screen you actually have? (That seems to be what Kurt-Dekker is assuming you want.)

    Not entirely clear what you mean by "make the FOV like 16:9".
     
  4. tomang5

    tomang5

    Joined:
    Feb 18, 2020
    Posts:
    63




    I'm not talking about UI. As it can be seen from the images, 16:9 cover that much area of 2K sprite (red area). For 21:9, it exceeds the sprite. To be able to fit 21:9 on the screen, I need to change the camera size. In this example, the camera size is 5. If the aspect ratio is 21:9. the size of 4 looks okay. I need an algorithm to make this automatically. It should increase the size if necessary. As it can be seen, for 4:3, the size must increase.

    There is also another thing to be consider, it shouldn't exceed the top and the bottom of the sprite as well.
     
    ROBYER1 likes this.
  5. m_a_sigouin

    m_a_sigouin

    Joined:
    May 16, 2017
    Posts:
    1
    I have a similar problem where changing the aspect ratio from 16:9 to 16:10 will hide elements of my fight scene on the left and right sides and I am struggling to find a solution to automatically change my camera size so that I can see all the elements of the scene.
     
    Last edited: Jan 31, 2023
  6. Magasenakwa

    Magasenakwa

    Joined:
    Oct 13, 2018
    Posts:
    86
    Me also. So glad someone resurrected this old thread
     
    ClearRoseOfWar likes this.
  7. Jar_Coding

    Jar_Coding

    Joined:
    Oct 5, 2017
    Posts:
    16
    If someone find this thread in the future and need a quick solution for this case <3

    This is a tiny script you can put on your camera, it will resize the camera rect to fit a given targetAspectRatio.

    Code (CSharp):
    1. [RequireComponent(typeof(Camera))][ExecuteAlways]
    2. public class AspectRatioCameraFitter : MonoBehaviour
    3. {
    4.     [SerializeField] private Camera cam;
    5.    
    6.     private readonly Vector2 targetAspectRatio = new(16,9);
    7.     private readonly Vector2 rectCenter = new(0.5f, 0.5f);
    8.  
    9.     private Vector2 lastResolution;
    10.  
    11.     private void OnValidate()
    12.     {
    13.         cam ??= GetComponent<Camera>();
    14.     }
    15.  
    16.     public void LateUpdate()
    17.     {
    18.         var currentScreenResolution = new Vector2(Screen.width, Screen.height);
    19.  
    20.         // Don't run all the calculations if the screen resolution has not changed
    21.         if (lastResolution != currentScreenResolution)
    22.         {
    23.             CalculateCameraRect(currentScreenResolution);
    24.         }
    25.  
    26.         lastResolution = currentScreenResolution;
    27.     }
    28.  
    29.     private void CalculateCameraRect(Vector2 currentScreenResolution)
    30.     {
    31.         var normalizedAspectRatio = targetAspectRatio / currentScreenResolution;
    32.         var size = normalizedAspectRatio / Mathf.Max(normalizedAspectRatio.x, normalizedAspectRatio.y);
    33.         cam.rect = new Rect(default, size) { center = rectCenter };
    34.     }
    35. }
     
  8. magitekxzone

    magitekxzone

    Joined:
    Feb 21, 2020
    Posts:
    2
    Fixed the Vector2 declaration
    Thanks this worked like a charm!
     
    lmartinignacio likes this.
Thread Status:
Not open for further replies.