2
3

More than 1 year has passed since last update.

unity Android でステータスバーの高さを取得する方法

Last updated at Posted at 2022-04-13

Screenshot_20220414-002120 (1).png

unityでAndroidにビルドする際、コンテンツが隠れないようにするため、ステータスバー(通知とか充電の部分)の高さを取得したいことがあります。
明確な記事がなかったので、その方法を記載しておきます。

コード

.cs
#if !UNITY_EDITOR && 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"))
        {
            using (var view = window.Call<AndroidJavaObject>("getDecorView"))
            {
                using (var rect = new AndroidJavaObject("android.graphics.Rect"))
                {
                    view.Call("getWindowVisibleDisplayFrame", rect);
                    Debug.Log(rect.Get<int>("top"));
                }
            }
        }
    }
}
#endif

補足

Debug.Log() のところを適当に弄ってください。

単位はピクセルです。Screen.CurrentResolution.height とかで割ってあげれば、画面に対するステータスバーの割合を取得できるかと思います。

2
3
0

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
  3. You can use dark theme
What you can do with signing up
2
3