You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity安卓应用始终显示状态栏与导航栏的实现问题

解决Unity Android状态栏透明后内容被遮挡的问题
  • 利用Unity内置安全区域适配
    给主Canvas添加Safe Area组件,它会自动识别状态栏、导航栏的区域,将UI内容限制在安全区域内。如果是UGUI布局,直接给根Canvas挂载该组件即可;如果是3D场景,需要手动调整相机视口或场景物体位置,避开顶部状态栏区域。

  • 修改AndroidManifest.xml主题配置
    打开你使用的自定义Manifest,找到UnityTransparentStatusBarTheme主题,添加android:fitsSystemWindows="true"属性,让系统知晓布局需要避开状态栏区域。示例代码:

    <style name="UnityTransparentStatusBarTheme" parent="@style/UnityThemeSelector">
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:fitsSystemWindows">true</item>
    </style>
    

    同时检查Manifest中是否存在android:windowFullscreen这类全屏属性,如有则移除或设为false。

  • 通过C#代码动态调整窗口布局
    编写Unity与Android原生交互的代码,强制设置窗口布局参数,让内容从状态栏下方开始显示。示例代码:

    using UnityEngine;
    
    public class StatusBarLayoutFix : MonoBehaviour
    {
        void Start()
        {
            #if UNITY_ANDROID && !UNITY_EDITOR
            AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            AndroidJavaObject window = currentActivity.Call<AndroidJavaObject>("getWindow");
            AndroidJavaClass layoutParamsClass = new AndroidJavaClass("android.view.WindowManager$LayoutParams");
            
            // 清除全屏标记
            window.Call("clearFlags", layoutParamsClass.GetStatic<int>("FLAG_FULLSCREEN"));
            // 设置布局稳定模式,适配系统窗口
            AndroidJavaObject decorView = window.Call<AndroidJavaObject>("getDecorView");
            decorView.Call("setSystemUiVisibility", layoutParamsClass.GetStatic<int>("SYSTEM_UI_FLAG_LAYOUT_STABLE"));
            #endif
        }
    }
    

    将该脚本挂载到场景的常驻GameObject上即可生效。

  • 检查Player Settings的细节配置
    打开Unity Player Settings -> Android -> Resolution and Presentation,确保Render Outside Safe Area选项未勾选;同时确认Default Orientation设置符合需求,部分特殊方向可能导致布局适配异常。

内容的提问来源于stack exchange,提问作者Jim Mede

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 04:30:56