using UnityEngine; #if UNITY_WEBGL && !UNITY_EDITOR using System.Runtime.InteropServices; #endif public class FullscreenController : MonoBehaviour { // Assign this to your Button OnClick public void GoFullscreenPortrait() { #if UNITY_WEBGL && !UNITY_EDITOR try { WebGL_RequestFullscreenAndPortrait(); } catch { } #else ForcePortraitNative(); #endif } #if UNITY_WEBGL && !UNITY_EDITOR // NOTE: name matches the JS function below (fixed the typo) [DllImport("__Internal")] private static extern void WebGL_RequestFullscreenAndPortrait(); #endif #if !UNITY_WEBGL || UNITY_EDITOR private void ForcePortraitNative() { // Allow only portrait Screen.autorotateToPortrait = true; Screen.autorotateToPortraitUpsideDown = false; Screen.autorotateToLandscapeLeft = false; Screen.autorotateToLandscapeRight = false; Screen.orientation = ScreenOrientation.Portrait; // or ScreenOrientation.AutoRotation with only portrait enabled // Fullscreen Screen.fullScreenMode = FullScreenMode.FullScreenWindow; Screen.fullScreen = true; #if UNITY_ANDROID EnableAndroidImmersive(); // hide status/nav bars #endif } #endif #if UNITY_ANDROID // Immersive sticky for Android private void EnableAndroidImmersive() { using var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); using var activity = unityPlayer.GetStatic("currentActivity"); using var window = activity.Call("getWindow"); using var decorView = window.Call("getDecorView"); const int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100; const int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200; const int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400; const int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002; const int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004; const int SYSTEM_UI_FLAG_IMMERSIVE_STICKY = 0x00001000; int flags = SYSTEM_UI_FLAG_LAYOUT_STABLE | SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | SYSTEM_UI_FLAG_HIDE_NAVIGATION | SYSTEM_UI_FLAG_FULLSCREEN | SYSTEM_UI_FLAG_IMMERSIVE_STICKY; decorView.Call("setSystemUiVisibility", flags); } #endif }