killapp/Kill/Assets/Scripts/Base/AndroidStatusBarManager.cs
2026-03-30 16:25:00 +08:00

68 lines
3.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

public void ShowTransparentSystemUI()
{
#if UNITY_ANDROID && !UNITY_EDITOR
try
{
Debug.Log("尝试显示透明Android系统UI");
using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
// 检查 activity 是否为 null
if (activity == null)
{
Debug.LogError("当前活动对象为空,无法显示导航栏");
// 添加延迟重试机制
Invoke("ShowTransparentSystemUI", 0.5f);
return;
}
// 使用 runOnUiThread 确保在主线程执行
activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
{
using (var window = activity.Call<AndroidJavaObject>("getWindow"))
{
// 清除全屏标志
window.Call("clearFlags", 0x00000400); // WindowManager.LayoutParams.FLAG_FULLSCREEN
// 添加半透明状态栏和导航栏标志 (适用于API 19及以上版本)
if (IsAndroidApiLevelAtLeast(19))
{
window.Call("addFlags", 0x04000000); // WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
window.Call("addFlags", 0x08000000); // WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
}
// 对于API 21及以上版本设置状态栏和导航栏颜色为透明
if (IsAndroidApiLevelAtLeast(21))
{
window.Call("setStatusBarColor", 0x00000000); // 透明
window.Call("setNavigationBarColor", 0x00000000); // 透明
}
// 设置系统UI可见性
using (var decorView = window.Call<AndroidJavaObject>("getDecorView"))
{
var systemUiVisibility = decorView.Call<int>("getSystemUiVisibility");
systemUiVisibility &= ~(0x00000004 | 0x00000002); // 清除SYSTEM_UI_FLAG_HIDE_NAVIGATION和SYSTEM_UI_FLAG_FULLSCREEN
// 对于API 23及以上版本设置浅色状态栏图标深色背景下可见
if (IsAndroidApiLevelAtLeast(23))
{
systemUiVisibility |= 0x00002000; // SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
decorView.Call("setSystemUiVisibility", systemUiVisibility);
}
}
}));
}
Screen.fullScreen = false;
Debug.Log("已完成显示透明Android系统UI的尝试");
}
catch (System.Exception e)
{
Debug.LogError("显示透明系统UI时发生错误: " + e.Message);
}
#endif
}