search
LoginSignup
6

More than 5 years have passed since last update.

posted at

Unity上からAndroidのステータスバーをいじる メモ

■Unity上からAndroidのステータスバーをいじる

class StatusBarChanger {
    private static int newStatusBarValue;

    public static void Show() {
        #if UNITY_ANDROID
            setStatusBarValue(2048); // WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN
        #endif
    }

    public static void Hide() {
        #if UNITY_ANDROID
            setStatusBarValue(1024); // WindowManager.LayoutParams.FLAG_FULLSCREEN; change this to 0 if unsatisfied
        #endif
    }

    private static void setStatusBarValue(int value) {
        newStatusBarValue = value;
        using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
            using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {
                activity.Call("runOnUiThread", new AndroidJavaRunnable(setStatusBarValueInThread));
            }
        }
    }

    private static void setStatusBarValueInThread() {
        #if UNITY_ANDROID
        using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
            using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {
                using (var window = activity.Call<AndroidJavaObject>("getWindow")) {
                    window.Call("setFlags", newStatusBarValue, newStatusBarValue);
                }
            }
        }
        #endif
    }
}

■Androidネイティブプラグイン等の参考ページ

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
6