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

Feature Request Option to enforce aspect ratios

Discussion in 'Editor & General Support' started by Marscaleb, Aug 12, 2023.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    986
    I have a post about this subject that keeps getting attention, so it seems there are a lot of people looking for this feature.

    I'd like a feature accessible in the project settings to ENFORCE a given aspect ratio, or possibly a range of aspect ratios.
    I am not referring to the list of supported ratios that appear in the player when a game is launched, but I mean how the engine itself handles rendering when it is told to operate at any given resolution.

    When enabled, the engine would automatically pillarbox/letterbox the rendered image if it is being output to an aspect ratio outside of the given range. Thus, users can still have the capacity to adjust resolutions for their device (including running at their desktop's resolution) but the game will still run with the developer's intended view. The engine would render an image locked to the specified aspect ratio and fill the remainder of the screen/window with a filler color (default of black)

    Yes, there are options that a developer can employ to produce this effect. However:
    1) Many such options come at the expense of in-game performance
    2) Implementing such options takes up a lot of development time, particularly since testing requires producing a full build
    3) Properly testing these systems to their full capacity is not feasible for most developers as it requires owning several monitors with different aspect ratios and switching between multiple devices.

    Having this feature built into the engine would:
    1) Operate at engine levels no developer has access to, and thus can operate more efficiently than any other solution
    2) Require only a simple single setting to enable or to alter if needed
    3) Be a setting developers could trust to work correctly without testing it on different hardware.

    For many games, enforcing an aspect ratio is a critical feature. This most clearly applies to 2D games where increasing the aspect ratio will extend the players' view and show sections that may not have content created, or alternatively constricting the view and hiding content that the player needs to see.

    I envision this feature working most effectively by allowing the project settings to set a range of aspect ratios, eg, nothing wider than X, nothing narrower than Y. A single aspect ratio can be enforced by simply setting both to the same value. For best use, we should be able to enter custom values so as to compensate for rare resolutions, ideally in an easily read X:Y format rather than a decimal value.

    And if you want to be really super amazing, allow developers to also adjust what the "letterbox" color is, perhaps even replacing it at run-time with a custom texture.
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,833
    The thing is, if I had a new Bananafone Turbo Pro Gold Deluxe with an 8.9" screen in glorious 17:13 aspect ratio, I would throw out any game that didn't give me that bezel-to-bezel goodness.

    Sure, maybe your game gets away with adding some info paneling so that the main part is 16:9, or 4:3, but don't just do the lazy letterboxing for it. "Reactive" UIs are more than a fad, they're important in cross-platform compatibility. The extra room is useful for data, or for getting the touchscreen controls out of the way to see more game, or whatever.

    That said, Unity's core feature set is mostly about implementing the things that would be difficult for you to implement yourself, and the Asset Store is for all the rest of the things that would be difficult or uninteresting for *some* people to implement themselves, but can be built atop the core features.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Any game that isn't some ancient retro game that doesn't let me use my full screen resolution is quickly getting uninstalled and refunded.
     
  4. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    986
    Any game where you can't see the whole game area is going to get uninstalled and refunded, too.
     
  5. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,977
    Plenty of games do this actually, good ones even. You would be using the screen's native resolution, but you would be pillarboxing or letterboxing if the monitor is not 16:9.

    I think you can do it, easily. Editing the viewport Rect in a camera can result in pillar boxing or letter boxing, figure out the math and place the correct values there at runtime (and I guess all cameras you might be using as well) and it should just work.

    upload_2023-8-20_0-17-42.png
     
    Last edited: Aug 19, 2023
    Marscaleb likes this.
  6. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    986
    I will bet ten bucks to say anyone with that kind of attitude wouldn't give two craps about any game I make, regardless of whether or not it works at that aspect ratio.
    Honestly I can't imagine anyone like that every playing anything that shows the Unity logo when it boots up.

    This about far more than just the UI, which I do agree is an important aspect of a game.
    The simple truth is that there are some games that do not work with unanticipated aspect ratios.
    Imagine playing a 2D platformer. If the game is built around a standard 16:9 display and someone plays on an ultra-wide screen, either the engine will crop the top and bottom of the screen off, and the player won't be able to see platforms they can jump to and enemies will attack them from off-screen, or else the view will be extended beyond what was intended to be seen, and there will be gaps in the world view, items appearing/disappearing/activating/deactivating and all kinds of nonsense.
    But neither could the game be properly built to account for an ultra-wide display without sacrificing gameplay for everyone else, because if the game properly accounted for these wide displays then people using them would have an advantage because they are seeing more of the game world.

    Those ultra-wide displays (from a gaming perspective) are only designed around 3D games with cameras fixed to the player (first person, over the shoulder, orbit cam, etc) as they extend the view to include more peripheral vision. But they break games with 2D designs, overhead camera, and fixed perspectives.

    Did you not read my post, or did I fail to explain it properly?
    The options for creating assets to control the resolution are insufficient. There are different approaches that can used, but they all present their own problems. One of the most prominent problems is that they are more taxing on the system than what could be done by the engine itself, slowing down the game, over-rendering, and/or performing slower checks.
     
  7. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,977
    Does something like this not do what you're asking? If not, please explain:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class camaspect : MonoBehaviour {
    4.  
    5.     void Start() {
    6.         myCamera.GetComponent<Camera>();
    7.     }
    8.  
    9.     Camera myCamera;
    10.     float desiredAspect = 16f/9f;
    11.  
    12.     void Update() {
    13.         float screenAspect = (float)Screen.width / (float)Screen.height;
    14.  
    15.         float ratio = screenAspect / desiredAspect;
    16.  
    17.         float margin = Mathf.Abs(ratio - 1f) * 0.5f;
    18.  
    19.         if (ratio > 1f) { // screen is wider
    20.             margin /= ratio;
    21.             Camera.main.rect = new Rect(margin, 0.0f, 1.0f - margin * 2.0f, 1.0f);
    22.         }
    23.         else {
    24.             Camera.main.rect = new Rect(0.0f, margin, 1.0f, 1.0f - margin * 2.0f);
    25.         }
    26.     }
    27. }
    EDIT: You can also stack another camera behind your main camera, that has a certain color or image as a background.