Responsive UI based on phone orientation change

Hello everyone, I’m trying to develop a graphical interface that adjusts according to the device’s orientation. The elements remain the same, but their style is changed to fit the right UI for the new orientation.

To do this, I’ve created two USS, “landscapeUSS” and “portraitUSS,” and two TSS, “LandscapeTSS” and “PortraitTSS.” Naturally, in “LandscapeTSS,” only “landscapeUSS” is present, and the same goes for the other TSS.

I’m monitoring orientation changes through a script in the “Update()” function, checking “Input.DeviceOrientation.” When the device switches to “portrait,” I set “portraitTSS” in the settings panel, and vice versa.

Everything seems to “work,” but not entirely: unfortunately, when transitioning from “portrait” (the default orientation) to “landscape,” the UI elements do not rotate but remain in the same direction as “portrait.”

Is there a method to resolve this issue? Is there something missing in my code, or should I work around the problem by considering the “horizontal direction” differently?

I’m leaving these examples here, which are screenshots taken on the device:

Here, the device is in portrait mode:

while here, it’s in landscape mode:

Hi,

with a simple example (3 VisualElements and a Label, no style override), I was able to have to rotation between landscape/portrait working (i.e.: the label is correctly oriented when I rotate the device).

Are you using “Auto Rotation” in Player settings ?

Also, make sure your device also has “auto-rotate” On.

Sorry, I don’t have the “auto-rotate” enabled on my phone. Did u know if there is something I can do to check if the device has the “auto-rotate” enabled or force it to enable?

Thank you for your help

Hi , we currently do not expose this information, however you can use this Java code directly in c# to obtain that info:

static bool DeviceAutoRotationIsOn()
{
#if UNITY_ANDROID && !UNITY_EDITOR
using (var unityPlayer = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”))
{
var activity = unityPlayer.GetStatic(“currentActivity”);
var systemSettings = new AndroidJavaClass(“android.provider.Settings$System”);
var contentResolver = activity.Call(“getContentResolver”);
return systemSettings.CallStatic(“getInt”, contentResolver, “accelerometer_rotation”) == 1;
}
#endif
return true;
}

thank you really much, I will try it :smile: