Search Unity

Force camera aspect ratio 16:9 in viewport

Discussion in '2D' started by Kashir, Feb 11, 2016.

  1. Kashir

    Kashir

    Joined:
    Oct 8, 2015
    Posts:
    1
    Hello! Not sure if this should be here or in scripting.

    I want to force the aspect ratio of the camera in the viewport.
    I want to see the same limits editing and testing or playing.

    This is a new 2D project by default with just a 1920x1080 sprite at 0, 0, 0:
    http://i.imgur.com/3P2desm.png

    And this is what I get if I increase the size. The camera can't see all the sprite up and down:
    http://i.imgur.com/soDIi8x.png

    Sorry is this is pretty basic and simple.
    I actually tried some scrips for camera and no ones works for me as I want.

    Greetings!
     
    bilalakil likes this.
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Well, at the top left of your game view you can choose a resolution or aspect ratio to preview your game with.

    Enforcing the resolution when you build out the game is a different task. I would recommend using an overlay canvas to limit the visible screen space on larger devices. The canvas will respect your chosen preview aspect ratio or resolution, so you can design around that. Adding black bars on the outside of it, or whatever you're going for. The canvas has a Canvas Scalar component that manages how the canvas will scale with different aspect ratios and resolutions.
     
  3. quarag

    quarag

    Joined:
    Nov 3, 2013
    Posts:
    16
    If you want to display a scene with a fixed size independent of resolution
    Include this script into your camera.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. /// <summary>
    5. /// Skrypt odpowiada za usatwienie rozdzielczosci kemerze
    6. /// </summary>
    7. public class CameraResolution : MonoBehaviour
    8. {
    9.  
    10.  
    11.     #region Pola
    12.     private int ScreenSizeX = 0;
    13.     private int ScreenSizeY = 0;
    14.     #endregion
    15.  
    16.     #region metody
    17.  
    18.     #region rescale camera
    19.     private void RescaleCamera()
    20.     {
    21.  
    22.         if (Screen.width == ScreenSizeX && Screen.height == ScreenSizeY) return;
    23.  
    24.         float targetaspect = 16.0f / 9.0f;
    25.         float windowaspect = (float)Screen.width / (float)Screen.height;
    26.         float scaleheight = windowaspect / targetaspect;
    27.         Camera camera = GetComponent<Camera>();
    28.  
    29.         if (scaleheight < 1.0f)
    30.         {
    31.             Rect rect = camera.rect;
    32.  
    33.             rect.width = 1.0f;
    34.             rect.height = scaleheight;
    35.             rect.x = 0;
    36.             rect.y = (1.0f - scaleheight) / 2.0f;
    37.  
    38.              camera.rect = rect;
    39.         }
    40.         else // add pillarbox
    41.         {
    42.             float scalewidth = 1.0f / scaleheight;
    43.  
    44.             Rect rect = camera.rect;
    45.  
    46.             rect.width = scalewidth;
    47.             rect.height = 1.0f;
    48.             rect.x = (1.0f - scalewidth) / 2.0f;
    49.             rect.y = 0;
    50.  
    51.              camera.rect = rect;
    52.         }
    53.  
    54.         ScreenSizeX = Screen.width;
    55.         ScreenSizeY = Screen.height;
    56.     }
    57.     #endregion
    58.  
    59.     #endregion
    60.  
    61.     #region metody unity
    62.  
    63.     void OnPreCull()
    64.     {
    65.         if (Application.isEditor) return;
    66.         Rect wp = Camera.main.rect;
    67.         Rect nr = new Rect(0, 0, 1, 1);
    68.  
    69.         Camera.main.rect = nr;
    70.         GL.Clear(true, true, Color.black);
    71.        
    72.         Camera.main.rect = wp;
    73.  
    74.     }
    75.  
    76.     // Use this for initialization
    77.     void Start () {
    78.         RescaleCamera();
    79.     }
    80.    
    81.     // Update is called once per frame
    82.     void Update () {
    83.         RescaleCamera();
    84.     }
    85.     #endregion
    86. }
    87.  
    float targetaspect = 16.0f / 9.0f; - This is the aspect to 1920x1080

    if you have an image / scene height of 1080 is that regardless of the resolution to the 1080 you need to calculate it as follows:
    1080 / (Pixel per unit) / 2. For the 1080 value 5.4 (if pixel per unit) has a value of 100.

    Sorry for my bad english :)
     
  4. Deleted User

    Deleted User

    Guest

    Awesome!!! Thank you quarag for this script! I was searching hours for this solution. I can finally fit the UI to the 2D Sprites :))))
     
  5. DevBearGames

    DevBearGames

    Joined:
    Oct 15, 2019
    Posts:
    1
    ThankYou quarag !!!
     
  6. formatc2013

    formatc2013

    Joined:
    Jul 4, 2016
    Posts:
    30
    I faced a similar issue, and I got around it by setting the orthographic cam size:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class OrtoCamResizerByScreenSize : MonoBehaviour
    4. {
    5.     [SerializeField] private float multiplier = 2.82f;
    6.  
    7.     private void Awake()
    8.     {
    9.         Camera.main.orthographicSize = Screen.height / Screen.width * multiplier;
    10.     }
    11. }
    Played around with a few screen sizes, and 2.82ish just worked fine for a couple of devices.
     
  7. joshuatg777

    joshuatg777

    Joined:
    Aug 15, 2020
    Posts:
    6
    Is there a way to make it not stick to the edges and have a set-able size? (I really need this)
     
  8. mveerman

    mveerman

    Joined:
    Feb 9, 2021
    Posts:
    2
    Great script!

    How would you fix the canvas in this case?
     
  9. Deleted User

    Deleted User

    Guest

    Thanks, man!!!!
     
  10. kalibcrone

    kalibcrone

    Joined:
    Sep 4, 2015
    Posts:
    10
    If you are asking how to keep all canvas UI items in your game resized to the same size as the screen camera, this is what worked for me for my 3D game where I use a screen space canvas for my hud, and the above script for creating black bars (Thanks quarag).

    1. Use Canvas Screen Space - Camera (Drag your camera into render camera)
    2. Set Canvas Scaler UI Scale Mode to Scale with Screen and set the appropriate resolution reference.
    3. Set Canvas Scaler Screen match mode = Match width or height
     
    Klaudija8 likes this.
  11. heartbreakkid1

    heartbreakkid1

    Joined:
    Feb 4, 2020
    Posts:
    6
    if your game is in portrait mode, you can use this script which will add black bars on top/bottom or left/right depending on aspect ratio


    float targetaspect = 16.0f / 9.0f;
    float windowaspect = (float)Screen.height / (float)Screen.width;
    float scalefactor;
    if (windowaspect >= targetaspect)
    {
    scalefactor = windowaspect / targetaspect;
    Camera.main.orthographicSize = Camera.main.orthographicSize * scalefactor;
    }
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Even when you are necro-posting to six-year-old threads, the same rules still apply:

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You may edit your post above. When you do, I shall delete this notice.
     
    Eristen likes this.