Version: Unity 6.5 Alpha (6000.5)
LanguageEnglish
  • C#

AndroidApplication.onWindowInsetsChanged

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

A callback to detect changes in the window insets when the application is running.

Notes:

  • The callback is only supported on Android 11 (API 30) or later versions. On older devices, you need to detect changes in the insets manually as demonstrated in the following code example.
  • Querying insets manually using AndroidApplication.currentWindowInsets is a slow operation.
using UnityEngine;
using UnityEngine.Android;

public class Controller : MonoBehaviour { private bool InsetsCallbackSupported => AndroidBuild.Version.apiLevel >= 30; private bool StatusBars; private bool NavigationBars;

public void Start() { if (InsetsCallbackSupported) AndroidApplication.onWindowInsetsChanged += OnInsetsChanged; else { var insets = AndroidApplication.currentWindowInsets; StatusBars = insets.IsVisible(AndroidWindowInsets.Type.StatusBars); NavigationBars = insets.IsVisible(AndroidWindowInsets.Type.NavigationBars); } }

public void Update() { if (!InsetsCallbackSupported) { var insets = AndroidApplication.currentWindowInsets; var currentStatusBars = insets.IsVisible(AndroidWindowInsets.Type.StatusBars); var currentNavigationBars = insets.IsVisible(AndroidWindowInsets.Type.NavigationBars); if (StatusBars != currentStatusBars || NavigationBars != currentNavigationBars) { OnInsetsChanged(insets); StatusBars = currentStatusBars; NavigationBars = currentNavigationBars; } } }

public void OnDisable() { if (InsetsCallbackSupported) AndroidApplication.onWindowInsetsChanged -= OnInsetsChanged; }

void OnInsetsChanged(AndroidWindowInsets insets) { Debug.Log($"NavigationBars visible " + insets.IsVisible(AndroidWindowInsets.Type.NavigationBars)); Debug.Log($"StatusBars visible " + insets.IsVisible(AndroidWindowInsets.Type.StatusBars)); } }