unity 刘海屏适配问题

17 篇文章 0 订阅
10 篇文章 0 订阅

1.手机一般的新手机安卓10 ,安卓9刘海屏unity 都可以使用这个方法:
unity刘海屏一般适配方法

2.如果上述没有解决你的问题可以看看下面的方法了

如果你是游戏的中的Ui或者是游戏物体被刘海挡住了
那么你就要编写android接口代码了不会写android代码的可以去学了。
2.1首先将项目导出android工程

    可以编写anroid代码了:首先判断手机的SDK版本 一般分为是否是androidP和非andoidP

2.2 androidP 判断手机是否是刘海屏:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                DisplayCutout displayCutout = activity.getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
                if (displayCutout != null) {
                    // 说明有刘海屏
                    return true;
                }
            }

2.3非androidP 判断手机是否有刘海屏了

**
     * 判断vivo是否有刘海屏
     * https://swsdl.vivo.com.cn/appstore/developer/uploadfile/20180328/20180328152252602.pdf
     *
     * @param activity
     * @return
     */
    private static boolean hasNotchVIVO(Activity activity) {
        try {
            Class<?> c = Class.forName("android.util.FtFeature");
            Method get = c.getMethod("isFeatureSupport", int.class);
            return (boolean) (get.invoke(c, 0x20));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断oppo是否有刘海屏
     * https://open.oppomobile.com/wiki/doc#id=10159
     *
     * @param activity
     * @return
     */
    private static boolean hasNotchOPPO(Activity activity) {
        return activity.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
    }

    /**
     * 判断xiaomi是否有刘海屏
     * https://dev.mi.com/console/doc/detail?pId=1293
     *
     * @param activity
     * @return
     */
    private static boolean hasNotchXiaoMi(Activity activity) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("getInt", String.class, int.class);
            return (int) (get.invoke(c, "ro.miui.notch", 0)) == 1;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断华为是否有刘海屏
     * https://devcenter-test.huawei.com/consumer/cn/devservice/doc/50114
     *
     * @param activity
     * @return
     */
    private static boolean hasNotchHw(Activity activity) {

        try {
            ClassLoader cl = activity.getClassLoader();
            Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
            return (boolean) get.invoke(HwNotchSizeUtil);
        } catch (Exception e) {
            return false;
        }
    }


2.4然后根据这些判断就可以确定手机是否刘海屏。如果是androidP第一种方法基本就可以解决的 如果不能解决:
可以尝试如下代码可以在这里设置:
LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT:仅仅当系统提供的bar完全包含了刘海区时才允许window扩展到刘海区,否则window不会和刘海区重叠;
LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES:允许window扩展到刘海区(原文说的是短边的刘海区, 目前有刘海的手机都在短边,所以就不纠结了);
LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER:不允许window扩展到刘海区

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                DisplayCutout displayCutout = activity.getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
                if (displayCutout != null) {
WindowManager.LayoutParams pm =getWindow().getAttributes();  
pm .layoutInDisplayCutoutMode=WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVE;  
getWindow().setAttributes(pm );
}


2.5如果是非androidP 我们需要做出处理首先需要判断是哪一款手机刘海屏,然后更据这个手机类型获取刘海屏的宽高
再将所要适配的UI或者游戏物体移动刘海同等宽高就行了。
我这里需要适配的是UI ,我做的游戏是2D游戏,由于UI顶部被刘海挡住了,一般的新手机在unity设置下就型了但是 SDK版本低于androidP就不行了。
如下获取各种手机的宽高:

 /**
     * 华为获取刘海的尺寸
     *
     * @param context
     * @return
     */
    private static int[] getNotchSizeInHuaWei(Activity context) {
        int[] ret = new int[]{0, 0};
        try {
            ClassLoader cl = context.getClassLoader();
            Class hwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            Method get = hwNotchSizeUtil.getMethod("getNotchSize");
            ret = (int[]) get.invoke(hwNotchSizeUtil);
        } catch (ClassNotFoundException e) {
            Log.e("test", "getNotchSize ClassNotFoundException");
        } catch (NoSuchMethodException e) {
            Log.e("test", "getNotchSize NoSuchMethodException");
        } catch (Exception e) {
            Log.e("test", "getNotchSize Exception");
        }
        return ret;
    }


/**
     * OPPO获取刘海的尺寸
     *
     * @param context
     * @return
     */
    private static int[] getNotchSizeInOPPO(Activity context) {
        return new int[]{324, 80};
    }
        /**
     * VIVO获取刘海的尺寸
     *
     * @param context
     * @return
     */
    private static int[] getNotchSizeInVIVO(Activity context) {
        return new int[]{DeviceUtils.dp2px(context, 100), DeviceUtils.dp2px(context, 27)};
    }
    /**
     * 因为小米获取不到刘海区的size,所以认为不可绘制
     *
     * @param context
     * @return
     */
    private static int[] getNotchSizeInXiaoMi(Activity context) {
        return new int[2];
    }
 /**
     * int[0]值为刘海宽度 int[1]值为刘海高度
     */
    private static int[] getNotchSizeInAndroidP(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            if (activity != null && activity.getWindow() != null) {
                View decorView = activity.getWindow().getDecorView();
                if (decorView != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    WindowInsets windowInsets = decorView.getRootWindowInsets();
                    if (windowInsets != null && windowInsets.getDisplayCutout() != null) {
                        DisplayCutout cutout = windowInsets.getDisplayCutout();
                        return new int[]{DeviceUtils.getScreenWidthInPx(activity) - cutout.getSafeInsetLeft() - cutout.getSafeInsetRight(), cutout.getSafeInsetTop()};
                    }
                }
            }
        }
        return new int[2];
    }

    /**

整合一下代码就是

  public static MobileType GetliuhaiMobileType(Activity activity) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            Log.e("", "Androidp");
            return MobileType.NONE;
        } else {
            // 通过其他方式判断是否有刘海屏  目前官方提供有开发文档的就 小米,vivo,华为(荣耀),oppo
          
             if (hasNotchHw(activity)) {
                Log.e("", "HUAWEI");
                return MobileType.HUAWEI;
            } else if (hasNotchXiaoMi(activity)) {
                Log.e("", "XIAOMI");
                return MobileType.XIAOMI;
            } else if (hasNotchOPPO(activity)) {
                Log.e("", "OPPO");
                return MobileType.OPPO;
            } else if (hasNotchVIVO(activity)) {
                Log.e("", "VIVO");
                return MobileType.VIVO;
            }

            Log.e("", "不是刘海屏或者是Androidp");
            return MobileType.NONE;
        }
    }




    public enum MobileType
    {
        NONE,
        HUAWEI,
        OPPO,
        XIAOMI,
        VIVO
    }


 public static int[] GetWeightHigh(Activity activity)
    {
        NotchUtils.MobileType type= GetliuhaiMobileType(activity);
        int[] rec=new int[]{0,0};
         if(type== NotchUtils.MobileType.NONE)
         {

         }
         else if(type== NotchUtils.MobileType.HUAWEI)
         {
             rec= getNotchSizeInHuaWei(activity);
         }
         else if(type== NotchUtils.MobileType.OPPO)
         {
             rec=getNotchSizeInOPPO(activity);

         }
         else if(type== NotchUtils.MobileType.VIVO)
         {
             rec= getNotchSizeInVIVO(activity);
         }
         else if(type== NotchUtils.MobileType.XIAOMI)
         {
             rec=  getNotchSizeInXiaoMi(activity);
         }
         Log.e("","1:"+rec[0]+"****2:"+rec[1]);
        return rec;
    }


然后就是在unity调用这个GetWeightHigh接口了根据这个接口的参数来设置UI的位置或者游戏物体的位置

Unity调用这个接口我就不介绍了。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值