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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How do I force the game to run at a given aspect ratio?

Discussion in 'Editor & General Support' started by Marscaleb, Mar 27, 2022.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    996
    I mean, it's pretty obvious: if someone plays the game at some weird aspect ratio, things are going to appear wrong. Either they'll see things they shouldn't, or not see things they should.

    So how do I have my build restrict the aspect ratio?

    I found an option in the player settings for "supported aspect ratios" but even when I only have 16:9 selected when I play it on a 32:9 monitor I still have the game filling the whole screen. Either the setting is broken or it requires something else to be set at well.
     
  2. Carcophan

    Carcophan

    Joined:
    May 10, 2021
    Posts:
    79
    I had a similar question, posted yesterday. There are so many posts, so fast, that it gets buried before anyone can see/respond to it.

    If you find a solution, share the knowledge! :)
     
  3. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    996
    Hmm, I was looking through this documentation page and I notice that the "supported aspect ratios" actually applies to what appears in the resolution dialogue displayed at startup.
    And since my game is set to "Default is native resolution" I never see that dialogue.

    I'm starting to wonder if this just isn't' possible in Unity.
    Well, at least without some kind of custom code that would draw black bars over part of the screen or something. But I can't find any native options within the engine.
     
  4. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    576
    If you want to have real control over rendering you should render into render texture and blit it on the screen in any aspect you want.
     
  5. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    996
    That seems like a pretty extreme solution, but I actually had considered it.

    Well, I have found that if I disable "default is native resolution" and set a specific resolution in the option which then appears, the game will then run with blackbars to fit the monitor the way I want, so there's that.
    The only problem is that it thus forces it to render at that resolution, which means it may not be getting a pixel-perfect rendering for the monitor. I guess I could write a script to find the monitor's resolution and try to find what that would be, but I was hoping for a more out-of-the-box solution to this.
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,516
    How to get your game bombarded with negative reviews 101 :). It plainly sucks when you buy a game and it doesn't look right on your hardware. Some people will deal with it, but they sure won't be happy.

    On a more serious note, there was a bug report reported to us not that long ago about the supported aspect ratios: https://issuetracker.unity3d.com/is...e-player-is-running-and-resolution-is-changed

    I resolved the bug as won't fix. I've also removed that setting from Unity 2022.2+ as it was causing confusion and wasn't really useful.

    That said, there are several ways to deal with it. It really depends on the kind of the game you're making and the reason you're asking this question.

    Unless you're making a hardcore competitive game, just let the people use the real estate of the hardware they invested in. That means you just have to make sure your game UI doesn't break if you're running at certain aspect ratios, but it's pretty easy to test it out by running in windowed mode and resizing the window to weird shapes manually.

    If you're really dead set on making players see the same amount of the world, here are your options:

    1. Since the problem comes from people with wider monitors having higher horizontal field of view, you could obscure the sides of the screen with UI. You can use the extra real estate of the screen to display information that doesn't give competitive advantage but still gives the player the impression that you support their setup;
    2. Use Camera.rect (https://docs.unity3d.com/ScriptReference/Camera-rect.html) to introduce letter/pillar boxing to limit the game view to your supported aspect ratios. While this ends up being a better experience than straight up forcing non-native rendering resolution, players will still get annoyed when they see it;
    3. Use Camera.fieldOfView (https://docs.unity3d.com/ScriptReference/Camera-fieldOfView.html) to enforce constant horizontal field of view. Using this technique will make anyone with an ultra-wide display see the same amount of things horizontally, but wider displays will see less things vertically. This technique is employed by many competitive shooters as vertical field of view generally does not matter that much in those types of games.
     
    halley and Jiaquarium like this.
  7. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    996
    Okay, I get that there are a lot of games out there where a wider view doesn't make a game-breaking change. A first person shooter, or anything with a freely-following camera in a 3D world, sure.
    But let's bear in mind that ANYTHING with a fixed camera will cause problems if the aspect ratio is off; please don't treat this like a fringe issue.
    For example: every 2D game, full stop. Let me repeat that: Every 2D game made with Unity will break if the aspect ratio goes too far.
    No one wants to play a platformer where they can't see the bottom of the screen to know if it is safe to jump somewhere. Neither does someone want to play one where they can't see enemies shooting at them off screen. Puzzle games will look tacky when the default Unity-blue creeps in on the edges of the screen.

    On top of that, there are still many 3D games with fixed camera angles that just don't play well with unexpected aspect ratios. A basic top-down view with the wrong aspect ratio can result in showing things the player isn't meant to see.

    And it is not just an issue with ultra-wide monitors. Current Steam user statistics show that there are still users with non-wide monitors.

    Please do not treat this issue as a rare thing that only a few users will have problems with; this impacts a significant percentage of Unity titles.
     
  8. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    996
    I've been running some experiments, and this is what I've found.
    If I go to the project settings and set Fullscreen Mode to "Fullscreen Window" and deselect "Default is Native Resolution" then I can enter a resolution to use by default, and when the game runs it will actually run as fullscreen to whatever the resolution of the monitor is (probably is actually whatever is set in Windows) but the rendered image will be at whatever resolution I typed in as the default settings.
    So effectively, the game will run in full screen but with black bars to lock the aspect ratio.

    I also found that with this mode I can set the resolution in-game to any arbitrary value, not just a standard monitor resolution. As such, the resolution could be lower than the monitor and look fuzzy. When I increased the resolution beyond what the monitor was displaying, I could not discern any increase in quality. Either the engine is locking the render resolution to not exceed the display, or the increased resolution is indistinguishable from just running FSAA. (Personally I think it's the former, as my UI didn't change scale either.)

    To get around this, I could have a script run at the game startup to change the screen resolution to match whatever the player's screen is, but trimmed to a value that maintains the desired aspect ratio. Thus, if the player has a 4K screen and I set that default value to 1080P, that script will start running the game at 4K instead of 1080P.

    Furthermore, I can have a setting in the game's options to reduce the rendered resolution as a percentage of the display resolution, to improve performance. A simple slider that could take the resolution down to... 25% maybe?

    Thus, I could lock the display to a given aspect ratio, still render to the highest resolution within that ratio, and allow the player to reduce the resolution to improve performance.
    From what I've read, not running in exclusive fullscreen will actually cost a bit of performance, but it looks like that will only be notable for more high-end titles that need to squeeze more power out of the system.

    I'd love to take a look at what I can do with that camera.rect option Tautvydas-Zilys recommended, but today is the last day I'm going to be borrowing my brother's ultra-wide monitor, so I'm not sure I can do any more definitive tests for a while.
     
  9. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,516
    I disagree. Games like Age of Empires II definitive edition or Stardew Valley work fine with increased field of view. Hundreds of other games work great on ultrawide monitors (and there are plenty of 2D/platformer games in that list).

    I don't think we're ignoring the problem, we just think that it's up to individual developer to decide what's best for their game. By default, we run at native aspect ratio because it gives the best gaming experience. However, we also provide tools like Screen.SetResolution or Camera.rect if you want to change that experience.

    You can test it by running the game in windowed mode with resizable border and just resize it to be short and wide.
     
    halley and Ryiah like this.
  10. Whitepot

    Whitepot

    Joined:
    May 19, 2017
    Posts:
    15
    We are building a game that will run at art exhibitions on a fixed 5:4 ratio arcade machine, and our client not having a 5:4 portrait screen to run test builds on is causing considerable confusion. Removing the functionality altogether and marking it as 'won't fix' is not suitable for our use case.

    I understand our scenario is most likely an edge case and a niche requirement, but dismissing the need for the feature as 'How to get your game bombarded with negative reviews 101 :)' is not a helpful official Unity response.
     

    Attached Files:

    Last edited: Aug 20, 2022
  11. jhughes2112

    jhughes2112

    Joined:
    Nov 20, 2014
    Posts:
    172
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,516
    If you just want to test the resolution rather than enforce it, just call Screen.SetResolution with desired parameters at startup. That's a very different use case from preventing people from changing it.
     
  13. Whitepot

    Whitepot

    Joined:
    May 19, 2017
    Posts:
    15
    To clarify, I want to enforce that aspect ratio regardless of screen resolution.
     
    Marscaleb and zoeyducoeur like this.
  14. Jiaquarium

    Jiaquarium

    Joined:
    Mar 22, 2020
    Posts:
    40
    Unfortunately, I banged my head into this too, and the only thing that worked was doing a pillarboxing / letterboxing solution with black bars where you calc a new camera Rect.
     
  15. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,184
    What you're describing is in my opinion a sign of a bad/lazy developer. For the last major project I worked on one of my tasks was dealing with the possibility that some elements may very well go off screen in unusual cases, and the solution is trivially simple: you create the playfield with the expectation that most people won't see all of it.

    squaby-different-devices-zoomToBiggerSide.jpg

    There is a ton of information available on this subject with even the most basic of searches thanks to this problem having been a solved one for decades now. Letterboxing of course works but it's a solution that can annoy some of your audience even if kept to a minimum.

    My group recently had a similar situation while trying to get a potential client interested. We were just about to mail them a device (an Oculus Quest 2) when they decided to just buy one themselves. A quick check of Amazon shows renewed 5:4 monitors available for around $110.
     
    Last edited: Oct 1, 2022
    akast07 likes this.
  16. audiodreadofficial

    audiodreadofficial

    Joined:
    Aug 10, 2021
    Posts:
    8
    I agree, I had two use cases that required workarounds for this issue. One was a game that was created in the style of a GameBoy Advance emulator and since GBA has a 16:10 screen allowing any other resolution not only messed up the authenticity of the feel but also completely messed with the art for certain battle screens that needed to take up the entire screen on a fixed camera. My most recent use case has to do with certain boss fights with fixed camera arenas that are meant to take up the whole screen. Are there ways around this? Yes. But esspecially for small teams with low budgets there are a good number of situations where the functionality would be incredibly useful and the notion that it's guarenteed to get your game bombarded with said reviews is a tad ridiculous.
     
    PowerJake, blueivy and zoeyducoeur like this.
  17. WF_Bart

    WF_Bart

    Joined:
    Apr 16, 2014
    Posts:
    29
    Very rude and uncalled for. Have you considered there are ultra-wide displays with extreme aspect ratios and games with cinematic styles that call for very specific framing to look right?
     
    Ruslank100, BigToe, Marscaleb and 7 others like this.
  18. Nichathan

    Nichathan

    Joined:
    Mar 8, 2020
    Posts:
    23
    I made a YouTube video and a GitHub tutorial that solves this issue with a second Camera, a Render Texture and a RawImage.

    Pretty much, by assigning a Render Texture to your camera, you are essentially able to force a given aspect ratio onto that camera.

    Youtube

    GitHub
     
    blueivy likes this.
  19. Fatalmasterpiece

    Fatalmasterpiece

    Joined:
    Nov 27, 2021
    Posts:
    2
    Holy cow. I have been facing this issue with my game recently. It is a 2D game with strictly 16:9 pre-rendered art and uses parallax for depth. Short of pillar boxing, there simply aren't assets to display widescreen and it looks very bad when the window is stretched because you can see beyond the edge of assets. This thread shows this isn't an isolated problem. I also need to "enforce that aspect ratio regardless of screen resolution."
    I figured I was doing something wrong but had no idea that Unity's official stance on this problem was basically "deal with it". I'm disappointed to say the least.
     
  20. Fatalmasterpiece

    Fatalmasterpiece

    Joined:
    Nov 27, 2021
    Posts:
    2
    It is kind of laughable that in 2023 Unity can't do this natively. It's very surprising that such a basic function isn't available and they have no plans to implement it.
     
  21. cvbattum

    cvbattum

    Joined:
    Apr 20, 2018
    Posts:
    12
    One very realistic use case for this is testing. We have a little game we are developing, and in the current prototype we're don't have the time to support every aspect ratio. However we do have certain aspect ratio dependent UI elements that we kinda wanna lock into place by enforcing one certain ratio. Kind of a shame that the engine won't easily support this.
     
  22. MaxGeorg

    MaxGeorg

    Joined:
    Nov 23, 2021
    Posts:
    4
    We also really would appreciate a more developer friendly approach to aspect ratios and resolutions.
    Because we're mostly producing interactive exhibits, we encounter all kinds of odd or even not so odd aspect ratios. It's a bit prohibitive to always have to use workarounds to publish reliable working solutions to our customers.
    We're using the pro version.
     
    Marscaleb and blueivy like this.
  23. CheeseMunchy

    CheeseMunchy

    Joined:
    Aug 11, 2019
    Posts:
    1
    Pretty ridiculous that that unity doesn't have a simple fix for this. A certain unity competitor is looking more and more attractive by the day.. :|
     
    PowerJake, SrOgurman and blueivy like this.
  24. Rispat-Momit

    Rispat-Momit

    Joined:
    Feb 14, 2013
    Posts:
    261
    This might help :)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ScreenRatioSetUp: MonoBehaviour
    6. {
    7.     public bool fullScreen = true;  // Whether the game should be in fullscreen mode
    8.  
    9.     private void Start()
    10.     {
    11.         // Calculate the target height based on the screen width and 16:9 aspect ratio
    12.         int targetHeight = Screen.width * 9 / 16;
    13.  
    14.         // Set the game's resolution to match the target width and height
    15.         Screen.SetResolution(Screen.width, targetHeight, fullScreen);
    16.     }
    17. }
    18.  
     
    chrismarch, mcaisw and blueivy like this.
  25. BlueAndRedGames

    BlueAndRedGames

    Joined:
    Jul 21, 2020
    Posts:
    2
    Yeah, def need a solution. I'm making a fixed camera horror game like old Resident Evil and the cameras are framed very intentionally and need to maintain 16:9 otherwise important stuff leaves the frame.
     
    Marscaleb likes this.
  26. BlueAndRedGames

    BlueAndRedGames

    Joined:
    Jul 21, 2020
    Posts:
    2
    Can you put it back? It's really screwing my game.
     
    juanmaguerrero_ likes this.
  27. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,516
    That setting did not do what you think it did. See the resolution note on the bug itself.
     
  28. stuartfdrake

    stuartfdrake

    Joined:
    Jan 15, 2023
    Posts:
    1
    No fix for the originally stated OP issue though? Been over a year. Still "won't fix" because it's not deemed necessary for anybody, despite more and more people coming into the same thread after obviously googling what they're trying to get done?
     
    juanmaguerrero_ and blueivy like this.
  29. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,516
    See my first reply. The original issue was a misunderstanding of what that setting did. "Fixing" it as OP wanted would involve developing a whole new feature that doesn't exist in the engine today. Given there are fairly simple ways of making it do what you want (I outlined 3 examples in my first reply), this has not been a priority.
     
  30. KurrandorSwap

    KurrandorSwap

    Joined:
    Feb 9, 2020
    Posts:
    2
    Fraccas likes this.
  31. VincentZhou1988

    VincentZhou1988

    Joined:
    Jun 6, 2022
    Posts:
    1
    Been dealing with this issue for quite some time... Tried every settings still no perfect solution. Ran into this thread, seeing that Unity really has no official solution for this, we ended up modifying the camera rect, plus adding an extra layer of black UI as pillarbox to prevent touch events from outside camera rect. It's still disappointing to know that you have to implement your solution yourself for such a common thing.
     
  32. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    996
    Editing the camera rect seems to be the most reasonable solution, but it still bugs me because it doesn't natively check the display resolution. If the resolution changes at run-time, that rect needs to be updated as well. It "sounds" easy to just put that check with with your in-game settings menu, but its still just begging for some code to get accidentally bypassed.
    It really should be built-in to the engine.
     
  33. Legendile7

    Legendile7

    Joined:
    Jun 22, 2021
    Posts:
    6
    Unity People, I understand y'all. But this is a fundamental feature that everyone wants. Google this topic, there are tons of places where people ask for it. It couldn't hurt to add support for this in Player Settings.
     
    Marscaleb and blueivy like this.