I want to find a way to detect if the Android navigation bar (the 3 buttons at the bottom of the screen) is visible, but I'm unable to find a solution. It's worth noting that my project is configured to run in full-screen mode with the 'hide navigation bar' option set to true. The problem is that precisely when the buttons appear (they appear for a few seconds and then hide again), the code is unable to detect this. I've tried many ways but I can't seem to detect this change (by change, I mean the navigation bar going from hidden to visible for a few seconds). I also want to add that my idea of detecting when these buttons are visible on the screen came about because I wanted to place an image when the application goes into the background so that the information in the application is hidden before Android takes the screenshot that appears in the 'recent apps' section. However, Unity was loading the image slower than Android was taking the screenshot. So, I thought of detecting when these buttons are visible on the screen because it could suggest that the user is about to minimize the application and load the image a few milliseconds earlier, making Unity faster than Android. I tried OnApplicationPause and OnApplicationFocus, but none of that works, as I mentioned earlier, Unity is slower than Android. So, either a way to detect these buttons or to change the screenshot that Android takes would work for me, as either of these two solutions would be helpful. I'll leave the code I currently have that detects the buttons ONLY if the Unity 'hide navigation bar' option is set to false.
using UnityEngine;
public class DetectButtons: MonoBehaviour
{
[SerializeField] private UnityEngine.UI.Text Text;
private static UnityEngine.UI.Text TextStatic;
private bool isNavBarVisible = false;
private void Start()
{
AndroidJavaObject unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject decorView = currentActivity.Call<AndroidJavaObject>("getWindow").Call<AndroidJavaObject>("getDecorView");
SystemUiVisibilityChangeListener listener = new SystemUiVisibilityChangeListener();
decorView.Call("setOnSystemUiVisibilityChangeListener", listener);
}
private void Update()
{
bool currentNavBarVisible = IsNavBarVisible();
if (currentNavBarVisible != isNavBarVisible)
{
isNavBarVisible = currentNavBarVisible;
if (isNavBarVisible)
{
Text.text = "Visible";
}
else
{
Text.text = "Hide";
}
}
}
private bool IsNavBarVisible()
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject decorView = currentActivity.Call<AndroidJavaObject>("getWindow").Call<AndroidJavaObject>("getDecorView");
int SYSTEM_UI_FLAG_FULLSCREEN = 4;
int visibility = decorView.Call<int>("getSystemUiVisibility");
return (visibility & SYSTEM_UI_FLAG_FULLSCREEN) == 0;
}
public static UnityEngine.UI.Text GetTextGameObject()
{
return TextStatic;
}
}
public class SystemUiVisibilityChangeListener : AndroidJavaProxy
{
public UnityEngine.UI.Text Text = DetectButtons.GetTextGameObject();
public SystemUiVisibilityChangeListener() : base("android.view.View$OnSystemUiVisibilityChangeListener")
{
}
public void onSystemUiVisibilityChange(int visibility)
{
int SYSTEM_UI_FLAG_FULLSCREEN = 4; //SYSTEM_UI_FLAG_FULLSCREEN
if ((visibility & SYSTEM_UI_FLAG_FULLSCREEN) == 0)
{
Text.text = "Visible";
}
else
{
Text.text = "Hide";
}
}
}
That code is what I have so far, but it doesn't work.
I don't have anything else to elaborate on the issue. I believe I've explained exactly what's happening and what I need.