- Home /
Execute code before the Made With Unity splash screen
My app supports both Android mobile and Android TV devices. I use Portrait mode for mobile devices, while I have to use Landscape mode for Android TV devices.
I had achieved this by setting the default orientation to Auto Rotation and running this in my first script (have this script as -400 in Script Execution Order settings):
private void InitOrientation()
{
UsesLandscape = AppHelper.IsTvDevice;
Screen.orientation = UsesLandscape ? ScreenOrientation.LandscapeLeft : ScreenOrientation.Portrait;
Screen.orientation = ScreenOrientation.AutoRotation;
Screen.autorotateToLandscapeLeft = UsesLandscape;
Screen.autorotateToLandscapeRight = false;
Screen.autorotateToPortrait = !UsesLandscape;
Screen.autorotateToPortraitUpsideDown = false;
}
This was working pretty well using Unity 5.5.1, but now I've upgraded to 5.5.2 and the Unity splash screen starts in Landscape mode even if my phone is oriented as Portrait. When my first scene starts, it runs the code above and goes to the correct orientation. But this causes a nasty orientation change between the Unity splash and my first scene on mobile phones.
I would like to run the code above before the Unity splash screen starts. Is this possible? Is there any other way to support different orientations depending on the device the app is running?
Answer by noobogami · Nov 20, 2021 at 12:20 PM
there is a attribute you can use for this
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]