UI changes wrong when Screen changes

Hello!

A few months ago I asked a question about Sprites that change its scale when the Screen changes its scale.
The correct answer was:
Use Camere.aspect = …

But now I realized that this only works for Sprites and not for the UI.

I tried many things.
But nothing worked.

Please help me.

Here are some screenshots:

[136563-снимок-экрана-2019-04-17-в-125743.png|136563]
after the change of Screen
[136564-снимок-экрана-2019-04-17-в-125858.png|136564]
But there is something interesting:
If you press pause and then change the size everything is good.
But if you then press play again
This happens(picture 2).

I solved this problem!

I found out that a Canvas scaler has a scale factor in Constant pixel mode.

I used this code for it:
IEnumerator ReCanvas()
{

        while (true)
        {
            cnv.GetComponent<CanvasScaler>().scaleFactor = 1f / (900f / Screen.height);
            yield return new WaitForSeconds(0.1f);


        }
    }

if you want to use this code:
change the 900!
I use 900 because I need this Size.
You can find out which one you need by doing this:

Set the Size of the GameWindow to your standart;
print it in the code;
round it;
this is your value you need to use instead of 900.

I know that not only I had this question:
Feel free to ask if you need help.