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 keep aspect ratio with resizable window?

Discussion in 'Editor & General Support' started by ftj89, Apr 15, 2021.

  1. ftj89

    ftj89

    Joined:
    Jan 2, 2020
    Posts:
    3
    Hi,

    In the "Standalone Player Options" I've set it so the user can resize the window and that I only support 16:9 aspect ration.

    When I resize the window the camera isn't sticking to this aspect ratio and I can see objects outside of it.
    Is there a way I can force the camera to keep to the 16:9 aspect ratio and not render anything outside of it.

    Cheers.
     
  2. ssls

    ssls

    Joined:
    Aug 26, 2020
    Posts:
    2
    I'm trying to find a way to fix this too. I also can't figure out how to maintain an aspect ratio with a non fullscreen window, while having it maximized. I'm on Windows 10 btw
     
  3. ftj89

    ftj89

    Joined:
    Jan 2, 2020
    Posts:
    3
    I figured it out in the end. Every time the screen size changed I set the camera's rect depending on the size of the screen.

    Code (CSharp):
    1.         var windowAspect = screenSize.x / screenSize.y;
    2.         var scaleHeight = windowAspect / TargetAspect;
    3.  
    4.         if (scaleHeight < 1)
    5.         {
    6.             Rect rect = cam.rect;
    7.  
    8.             rect.width = 1;
    9.             rect.height = scaleHeight;
    10.             rect.x = 0;
    11.             rect.y = (1 - scaleHeight) / 2;
    12.  
    13.             cam.rect = rect;
    14.         }
    15.         else
    16.         {
    17.             float scalewidth = 1 / scaleHeight;
    18.  
    19.             Rect rect = cam.rect;
    20.  
    21.             rect.width = scalewidth;
    22.             rect.height = 1;
    23.             rect.x = (1 - scalewidth) / 2;
    24.             rect.y = 0;
    25.  
    26.             cam.rect = rect;
    27.         }
     
  4. wmjoers

    wmjoers

    Joined:
    Jan 4, 2020
    Posts:
    3
    I found this thread when having the same issue and use the sample code to write a sample where you also can have a dynamic width and/or height.

    Check out this sample projekt https://github.com/wmjoers/CameraScaler! The CameraScaler script lets you keep the aspect ratio or you can let it grow in any direction depending on your needs. Also note that you need to use a second camera to make sure you get a black letter box (it's in the sample scene).
     
    PixelPockets, SystemId and c8theino like this.
  5. wmjoers

    wmjoers

    Joined:
    Jan 4, 2020
    Posts:
    3
    I also added a FullscreenHandler script to take care of issues when moving between windowed and fullscreen.
     
    SystemId and c8theino like this.
  6. fegabe

    fegabe

    Joined:
    Jan 3, 2015
    Posts:
    18
    Thank you very much for this solution! I don't know why is not something that could be set in project settings for 2D games at least since it will be very useful.

     
  7. enderandpeter

    enderandpeter

    Joined:
    Sep 16, 2015
    Posts:
    6
    I gave your code a try, but I was unsure about a few things. Are screenWidth and screenHeight the same as Screen.width and Screen.height? When making that switch and referring to the scene's main camera, sadly it did not result in maintaining the aspect ratio when the window was set to a very long width and short height.

    These scripts do indeed keep the aspect ratio no matter what extreme sizes the window is set to. I think this project would benefit from skimming down on the current files so that it can be added to a project's manifest.json.