- Home /
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:
after the change of Screen
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).
Answer by NikitaDemidov · Apr 17, 2019 at 03:29 PM
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.
Warning: I just realized that you have to use Pixel Perfect $$anonymous$$ode and Screen Space - Camera for that. Because else it doesn't check the Screen width.
Your answer
![](https://koobas.hobune.stream/wayback/20220612195639im_/https://answers.unity.com/themes/thub/images/avi.jpg)