6

I have a game project that uses a 9:16 aspect ratio. And also the canvas that will "Scale with Screen Size" with "Reference Resolution" 1080x1920 (9:16)

When I build the project and give some settings in "Player Settings" like this:
Player Settings

The results of the game are built, always just use "Free Aspect Ratio". Like this:

All game components, should be fit with the background

How can I build a project using only the aspect ratio that I want?

Thank you

2 Answers 2

4

I've had trouble getting custom aspect ratios in standalone builds as well.

You can set the screen width and height manually once in the start method.

void Start()
{
    Screen.SetResolution(1080, 1920);
}

If need be, you can also update it while the game is running

private float lastWidth;
private float lastHeight;

void Update()
{
    if(lastWidth != Screen.width)
    {
        Screen.SetResolution(Screen.width, Screen.width * (16f / 9f));
    }
    else if(lastHeight != Screen.height)
    {
        Screen.SetResolution(Screen.height * (9f / 16f), Screen.height);
    }

    lastWidth = Screen.width;
    lastHeight = Screen.height;
}

Unity Doc:
Screen
SetResolution()

3
  • I have 3 scene in my game. Where should I place this script? (i. e camera.main, etc)
    – Yunan Adil
    Mar 17, 2019 at 7:33
  • thank you for your reply. After I created the script for "void Start ()" above. My game can have the resolution that I want, but I tried it from another resolution size of "9:16", which is "360x640" and it works. Thanks And, I put this script, on every camera playing from all my game scenes. Note : "Screen.SetResolution" not only has 2 parameters, like the following documentation:docs.unity3d.com/ScriptReference/Screen.SetResolution.html
    – Yunan Adil
    Mar 17, 2019 at 9:22
  • You're right, Screen.SetResolution() has more than 1 overflow method, but you don't need to play with the refresh rate for this example. However, thank you for reminding to include the unity documentation, I forgot to do that.
    – Savaria
    Mar 17, 2019 at 18:41
2
void Start()
{
    SetRatio(3, 4);
}
void SetRatio(float w, float h)
{
    if ((((float)Screen.width) / ((float)Screen.height)) > w / h)
    {
        Screen.SetResolution((int)(((float)Screen.height) * (w / h)), Screen.height, true);
    }
    else
    {
        Screen.SetResolution(Screen.width, (int)(((float)Screen.width) * (h / w)), true);
    }
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.