Hello everyone, in an Android app I am building that is only UI I need to be able to reorient the display from landscape to portrait and back when an option is selected. This is working fine, I click the option in my settings and the right UI gets displayed.
The problem is when I am testing on an Android device, the reorientation also works fine for activating the right UI, but the display gets stretched out and does not fit the screen anymore.
I am using Unity Remote 5 for my testing.
So what happens exactly when I am testing is:
- I start my app and it starts correctly in Landscape orientation and gets displayed correctly through Remote 5.
- I go in the menu and change the orientation, now the app is in portrait mode.
- The right UI gets displayed but it is streched out.
- While the test is running, I need to go in the editor and from the game window I change the aspect from the menu from landscape to portrait and then and only then the UI is displayed correctly and not stretched out.
I tried to do the same changes in code that happens when you change the aspect ratio manually from the list in the game window in the editor, but it does not work without always having to resort to changing it manually in the editor.
Here is the method to change the orientation and where I call SetAndroidOrientation(resolution)
public void SetDisplayOrientation(bool isLandscape)
{
_isInLandscapeOrientation = isLandscape;
Vector2Int resolution = isLandscape ? ResolutionSettings.Horizontal : ResolutionSettings.Vertical;
if (Application.platform == RuntimePlatform.Android)
{
Screen.fullScreen = true;
SetAndroidOrientation(resolution);
}
else
{
Screen.SetResolution(resolution.x, resolution.y, false);
}
//...
This is my SetAndroidOrientation() method that gets called, I always have the same behavior even if I have only have Sreen.orientation = ScreenOrientation…; in each if statements
private void SetAndroidOrientation(Vector2Int resolution)
{
if (resolution == ResolutionSettings.Horizontal)
{
Screen.SetResolution(resolution.x, resolution.y, true);
Screen.orientation = ScreenOrientation.LandscapeLeft;
CanvasScaler canvasScaler = _userPanelsCanvas.GetComponent<CanvasScaler>();
canvasScaler.referenceResolution = resolution;
}
if (resolution == ResolutionSettings.Vertical)
{
Screen.SetResolution(resolution.x, resolution.y, true);
Screen.orientation = ScreenOrientation.Portrait;
CanvasScaler canvasScaler = _userPanelsCanvas.GetComponent<CanvasScaler>();
canvasScaler.referenceResolution = resolution;
}
}
When i am referencing to changing the aspect ratio manually through the editor in the game window I mean through here

I’ve been stuck on this problem for a while, I don’t know what to do now. How to have the same behavior as when I change the aspect ratio manually through the editor when I’m running a test with Unity Remote 5. Thanks for your help!