0
\$\begingroup\$

I'm encountering the strange behaviour that my UI (canvas with some buttons) is looking differently after the second launch on mobile. Restart of the phone does not help. This does not happen in play mode. This does not happen in play mode and only einstalling the app results again in the correct view. (installation is done via transfer of apk to phone and installing it). There are no errors in the console and everything works on the first start. The version used is 2021.1.15

In the editor the screen resolution is set to the same as the phone, the canvas scaler script is deactivated, there is no script that scales or adds elements dynamic or from a save file. All elements are places via the inspector.

I created a new project with the canvas and some buttons, looking like thisenter image description here enter image description here.

When I first time start the app, it looks exactly as in play mode: enter image description here

On second app launch, it looks like this: enter image description here

The numbers on the button are the position and size

GetComponentInChildren<Text>().text =
            transform.position.ToString() + GetComponent<RectTransform>().sizeDelta.ToString();

The button itself is default button added over the menu with no layout at all, values adjusted over the inspector. The original scene had more objects in the scene itself which where not affected by the second launch, only the UI gets messed up. The button has still the same reported size on the second launch but the position is way smaller.

Now if I add the canvas scaler, set it to scale with screen size and enter the same values as my phone uses, I'm getting a slightly different behaviour. The first launch is still exactly the same, on the second launch the buttons are still similar to the first launch but all the numbers are still from the second screenshot. While the UI displays all element, they are not as sharp as on the fist launch.

Why would the canvas think the phone has a resolution of 1493x720 on the second launch instead of the correct 2240x1080?

Update:

Here are the numbers between the two launches.

enter image description here

\$\endgroup\$
5
  • \$\begingroup\$ Please walk us through the steps to create a Minimal Complete Verifiable Example. Once we van reproduce your problem in a new, empty project, we can test potential solutions. \$\endgroup\$
    – DMGregory
    Jul 28 at 21:42
  • \$\begingroup\$ I have sometimes witnessed inconsistent behavior in the UI system when adding contradicting layout elements. But it doesn't look as if you have much automatic layouting going on here. How exactly did you set the positions of those buttons? \$\endgroup\$
    – Philipp
    Jul 29 at 11:29
  • \$\begingroup\$ Where do the numbers on the buttons come from? I would assume they are the position and size, but they are different in those two screenshots. So I assume you are setting them automatically. Where and how do you do that? \$\endgroup\$
    – Philipp
    Jul 29 at 11:32
  • \$\begingroup\$ The buttons are not set automtically, the script is just reporting the position of them in a single line as an attempt to find out why the second launch is different compared to the first. \$\endgroup\$
    – Zibelas
    Jul 29 at 11:34
  • \$\begingroup\$ Interesting! I wonder if your phone OS is somehow enabling a compatibility mode - looking at the results from the first launch, it (potentially wrongly) concludes your app has compatibility/performance problems at full resolution, so the next time it launches, it forces it to use a smaller resolution to "help"? Or it could be a Unity engine feature doing something similar. What values do you get from Screen.width, Screen.height, Screen.dpi, and Screen.resolutions if you query them in a script on each launch? \$\endgroup\$
    – DMGregory
    Jul 29 at 16:20
1
\$\begingroup\$

I still have not figured out what this is causing this, at least it is possible to force the game to use the correct resolution by just setting the resolution back.

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

https://docs.unity3d.com/ScriptReference/Screen.SetResolution.html

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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