Unger Games
Unger Games

Reputation: 11

Unity how to disable Fullscreen on Android devices (S8)

If I build my App and play it on my Samsung Galaxy S8 its always on Fullscreen Mode. I can´t even disable it vis Settings.

I tried it with the Code "Screen.fullScreen = false;" but it don´t work. In my playersettings the Aspect Ratio is at Custom (16.9).

Do you have any Idea how to solve this Problem? I want it not to be on fullscreen.

Upvotes: 0

Views: 6156

Answers (3)

radu_paun
radu_paun

Reputation: 2140

You need do disable it when you build the unity project:

Open Unity Editor: Open your Unity project.

Player Settings:

Go to "File" -> "Build Settings." In the Build Settings window, click on the "Player Settings" button. Android Player Settings:

In the Player Settings, go to the "Android" tab. Resolution and Presentation Settings:

Look for the "Resolution and Presentation" section. Uncheck the "Fullscreen Mode" option.

Upvotes: 0

Anrimian
Anrimian

Reputation: 4477

Unity in UnityPlayer.java always launches fullscreen mode without any conditions. There is a tricky way to prevent such call on android side. We need to return fake Window object during UnityPlayer constructor call.

class OurActivity: Activity {

    private var returnWindowStub = false

    override fun onCreate(savedInstanceState: Bundle?) {
        //..initialize

        returnWindowStub = true

        //initialize UnityPlayer here
        // = UnityPlayer(this)

        returnWindowStub = false
    }

    override fun getWindow(): Window {
        val window = super.getWindow()//return normal window object in all other cases
        return if (returnWindowStub) AppWindowWrapper(window) else window
    }

    class AppWindowWrapper(private val window: Window): Window(window.context) {

        //consume fullscreen flag
        override fun setFlags(flags: Int, mask: Int) {
            if (flags == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
                return
            }
            window.setFlags(flags, mask)
        }


        //then we need to override and proceed a lot other Window methods, like this
        override fun setEnterTransition(transition: Transition?) {
            window.setEnterTransition(transition)
        }

        override fun setReturnTransition(transition: Transition?) {
            window.setReturnTransition(transition)
        }
        //and more of them
    }
}

Upvotes: 0

Programmer
Programmer

Reputation: 125435

This was easy to do in the past by creating new UnityPlayer subclass and modifying the setFullscreen(true); to setFullscreen(false); but Unity API and Android version has changed a lot and this can't be done anymore that easy anymore.

You have to get current Unity activity from C# com.unity3d.player.UnityPlayer, get Android's WindowManager from it then clear the FLAG_FULLSCREEN flag.

This person has already done it and below is what his/her code looks like:

public static void SetupAndroidTheme(int primaryARGB, int darkARGB, string label = null)
{
    #if UNITY_ANDROID && !UNITY_EDITOR
    label = label ?? Application.productName;
    Screen.fullScreen = false;
    AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
    activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
    {
        AndroidJavaClass layoutParamsClass = new AndroidJavaClass("android.view.WindowManager$LayoutParams");
        int flagFullscreen = layoutParamsClass.GetStatic<int>("FLAG_FULLSCREEN");
        int flagNotFullscreen = layoutParamsClass.GetStatic<int>("FLAG_FORCE_NOT_FULLSCREEN");
        int flagDrawsSystemBarBackgrounds = layoutParamsClass.GetStatic<int>("FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
        AndroidJavaObject windowObject = activity.Call<AndroidJavaObject>("getWindow");
        windowObject.Call("clearFlags", flagFullscreen);
        windowObject.Call("addFlags", flagNotFullscreen);
        windowObject.Call("addFlags", flagDrawsSystemBarBackgrounds);
        int sdkInt = new AndroidJavaClass("android.os.Build$VERSION").GetStatic<int>("SDK_INT");
        int lollipop = 21;
        if (sdkInt > lollipop)
        {
            windowObject.Call("setStatusBarColor", darkARGB);
            string myName = activity.Call<string>("getPackageName");
            AndroidJavaObject packageManager = activity.Call<AndroidJavaObject>("getPackageManager");
            AndroidJavaObject drawable = packageManager.Call<AndroidJavaObject>("getApplicationIcon", myName);
            AndroidJavaObject taskDescription = new AndroidJavaObject("android.app.ActivityManager$TaskDescription", label, drawable.Call<AndroidJavaObject>("getBitmap"), primaryARGB);
            activity.Call("setTaskDescription", taskDescription);
        }
    }));
    #endif
}

public static int ToARGB(Color color)
{
    Color32 c = (Color32)color;
    byte[] b = new byte[] { c.b, c.g, c.r, c.a };
    return System.BitConverter.ToInt32(b, 0);
}

Example call:

SetupAndroidTheme(ToARGB(Color.black), ToARGB(Color.black));

Not tested on Samsung Galaxy S8 but it works on my Android device.

Upvotes: 1

Related Questions