Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build project in unity with custom aspect ratio?

Tags:

c#

unity3d

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

like image 782
Yunan Adil Avatar asked Mar 17 '19 07:03

Yunan Adil


People also ask

How do I change resolution in Unity project?

To do so, go to Edit | Project Settings | Player. Your Inspector should now display the following: You may have more or fewer platforms displayed here; it depends on the modules you have installed with Unity. To force a specific resolution on a PC, Mac, & Linux Standalone game, deselect Default is Native Resolution.


1 Answers

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()

like image 189
Savaria Avatar answered Oct 06 '22 03:10

Savaria