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
とかで割ってあげれば、画面に対するステータスバーの割合を取得できるかと思います。