Skip to content

Commit b4ced74

Browse files
committed
Added the script which adds support for rendering behind the notch in Android 9 Pie and later
1 parent c17010a commit b4ced74

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using UnityEngine;
2+
3+
public class RenderBehindNotchSupport : MonoBehaviour
4+
{
5+
public bool RenderBehindNotch = true;
6+
7+
// Constants from https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html
8+
private const int LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES = 1;
9+
private const int LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER = 2;
10+
11+
private AndroidJavaObject m_Window;
12+
private AndroidJavaObject m_Windowattributes;
13+
14+
void Start () {
15+
using (var version = new AndroidJavaClass("android.os.Build$VERSION"))
16+
{
17+
// Supported on Android 9 Pie (API 28) and later
18+
if (version.GetStatic<int>("SDK_INT") < 28)
19+
{
20+
return;
21+
}
22+
}
23+
using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
24+
{
25+
using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
26+
{
27+
m_Window = activity.Call<AndroidJavaObject>("getWindow");
28+
m_Windowattributes = m_Window.Call<AndroidJavaObject>("getAttributes");
29+
m_Windowattributes.Set("layoutInDisplayCutoutMode", RenderBehindNotch ?
30+
LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES :
31+
LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER);
32+
activity.Call("runOnUiThread", new AndroidJavaRunnable(ApplyAttributes));
33+
}
34+
}
35+
}
36+
37+
void ApplyAttributes()
38+
{
39+
if (m_Window != null && m_Windowattributes != null)
40+
m_Window.Call("setAttributes", m_Windowattributes);
41+
}
42+
}

0 commit comments

Comments
 (0)