Screen.SetResolution problem

I changed the resolution of my game with

Screen.SetResolution(1280, 720, true);

The problem is that in Android the screens have different ratios.
So change the aspect of the camera to solve it.

float xx = Screen.width;
float yy = Screen.height;
Screen.SetResolution(1280, 720, true);
camara.aspect = xx / yy;

Now I am having trouble with the GUI still having wrong aspect and it looks deformed.
Any ideas?

Try using Camera.ResetAspect()

Also, look at your canvas’s render mode. And your elements alignment. If any are set to stretch, then you will get what you call deformities. That is by design.

It didn’t work
And I don’t have any special settings in the GUI. The one that is by default.

These settings are not special. They define your rect transform behavior. For example, below is stretch(blue) vs anchor(red).

Oh! I get it. no I don’t use that type of configuration most of my GUI is hooked to the center with a certain size.
I should explain myself better, my GUI works well, it adapts to cell phone screens.
The problem arises specifically when I use

Screen.SetResolution(1280, 720, true);

Can you provide a screenshot of what it looks like before and after you change resolution?

I put the circles in the corners so that the deformations are more noticeable.
1)This is the canvas configuration.
2) It is the configuration of an UI element.
3) It is an image of a cell phone with resolution 2340 x 1080
As you can see, although the configuration was 1920 x 1080, it adapts well.
4) Now after running the code:

float xx = Screen.width;
float yy = Screen.height;
Screen.SetResolution(1280, 720, true);
camara.aspect = xx / yy;

The UI is elongated.




Hmmm… I see, yea that’s nothing related to what I mentioned.

Sorry, I have not played much with different resolutions before. My guess atm though would be your canvas settings.
If you switch match mode from width => height, to height => width. Do you screen deform the other direction?

You could try changing the canvas reference resolution to match your screen. However my understanding is that you should not have to, match mode takes care of that. Worth a try though.

So I just created a project to test it myself. Canvas and image settings are the same as yours.
Using the below code worked just fine. No issues. This was tested on version 2019.3.11f1

Have you tried making a new project and redoing the UI to see if it was a bug? Maybe try a different version too.

void Update()
{
    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        float xx = Screen.width;
        float yy = Screen.height;
        Screen.SetResolution(33440, 1440, true);
        Camera.main.aspect = xx / yy;
    }
    if (Input.GetKeyDown(KeyCode.DownArrow))
    {
        float xx = Screen.width;
        float yy = Screen.height;
        Screen.SetResolution(900, 900, true);
        Camera.main.aspect = xx / yy;
    }
}

to make sure the aspect does not change, just set the new resolution to be a multiple of the old, instead of using hard coded values.

  • float wid = Screen.width;
  • float hi = Screen.height;
  • float sizeMul = .5f;
  • Screen.SetResolution(wid * sizeMul, hi * sizeMul, true);
1 Like