2
\$\begingroup\$

I have made a game that was supposed to be for mobile but now I need to build it for PC. I want the aspect-ratio to be 2:3 but as you can see on the image that doesn't exist. How do I add a custom aspect-ratio for the build?

enter image description here

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You can programmatically set the aspect ratio.

https://docs.unity3d.com/ScriptReference/Screen.SetResolution.html

Here is some sample code from Unity:

using UnityEngine;

public class ExampleScript : MonoBehaviour
{
    void Start()
    {
        // Switch to 640 x 480 full-screen
        Screen.SetResolution(640, 480, true);
    }
}

If you want to set it via an aspect ratio instead of a resolution, you can do some math: Screen.SetResolution(Screen.height * (2.0f / 3.0f), Screen.height, true)

Here is a generic script that you can run when your application launches. Attach the script to a GameObject in your starting scene, and change aspect ratio in the inspector.:

using UnityEngine;

public class AspectRatioManager : MonoBehaviour
{
    [Tooltip("Aspect Ratio to use for game.  If Vector2.zero, the default aspect ratio will be used.")]
    [SerializeField]
    private Vector2 aspectRatio = Vector2.zero;

    [Tooltip("Whether or not full screen will be used")]
    [SerializeField]
    private bool fullScreen = false;

    private void Awake()
    {
        if(aspectRatio != Vector2.zero)
        {
            float x = Screen.height * (aspectRatio.x / aspectRatio.y);
            float y = Screen.height;
            Screen.SetResolution(x, y, fullScreen)
        }
    }
}

This code assumes you have a "tall" app on a "horizontal" monitor. Would need to make changes if that were not the case.

StackOverflow also has a nice answer to this question.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Given that 2:3 ratios are not common for PC monitors, you might want to default the third SetResolution argument to false, so you play in windowed mode, unless the player deliberately activates a full-screen option. \$\endgroup\$
    – DMGregory
    Commented May 6, 2020 at 14:10
  • \$\begingroup\$ @DMGregory Good call. Fixed. I actually want to write a more generic script now - sounds like fun. One that supports wide screen aspect ratio as well \$\endgroup\$
    – Evorlor
    Commented May 6, 2020 at 14:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .