Unity Android is compatible with notch screen, water drop screen, heterosexual screen

Mobile phone manufacturers are a group of trendsetters, and they can play new tricks every time, with all kinds of weird mobile phone screens. In order to increase the player's sense of immersion, we must adapt to the screen of the opposite sex when we develop games.

Generally, the Android method is adapted. In fact, there are many solutions on the Internet. The mainstream one is Google’s official interface, the hole-digging screen.

The first is the adaptation below the Android p version (apilv 28)

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P)
    {
        String result = getNotchByManufacturer();
           
        return;
    }
public static String getNotchByManufacturer()
    {
        Context context = UnityPlayer.currentActivity;
        String manufacturer = Build.MANUFACTURER.toLowerCase();

        String result = MANU_NONE;
        if(TextUtils.equals(manufacturer, "huawei"))
        {
            if (hasNotchInHuawei(context))
                result = NOTCH_TYPE_HUAWEI;
        }
        else if(TextUtils.equals(manufacturer, "xiaomi"))
        {
            if (hasNotchInMIUI(context))
                result = NOTCH_TYPE_MIUI;
        }
        else if(TextUtils.equals(manufacturer, "oppo"))
        {
            if (hasNotchInOppo(context))
                result = NOTCH_TYPE_OPPO;
        }
        else if(TextUtils.equals(manufacturer, "vivo"))
        {
            if (hasNotchInVivo(context))
                result = NOTCH_TYPE_VIVO;
        }
        else if(TextUtils.equals(manufacturer, "smartisan"))
        {
            if (hasNotchInSmart(context))
                result = NOTCH_TYPE_SMART;
        }
        else
            result = ApiUnsupported;

        return result;
    }

Generally, mobile phone manufacturers will provide adaptation solutions.

<meta-data
 android:name="notch.config"
 android:value="portrait|landscape"/>

Use this interface. Add a meta-data under Application, whether to use the ear area

Huawei: https://devcenter-test.huawei.com/consumer/cn/devservice/doc/50114
Xiaomi: https://dev.mi.com/console/doc/detail?pId=1293
Oppo: https:// open.oppomobile.com/service/message/detail?id=61876
Vivo: https://dev.vivo.com.cn/documentCenter/doc/103
 

public static boolean hasNotchInMIUI(Context context)
    {
        try
        {
            return SystemProperties.getInt("ro.miui.notch", 0) == 1;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * OPPO
     *
     * @param context Context
     * @return hasNotch
     */
    public static boolean hasNotchInOppo(Context context)
    {
        return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
    }

    /**
     * VIVO
     * <p>
     * android.util.FtFeature
     * public static boolean isFeatureSupport(int mask);
     * <p>
     * 参数:
     * 0x00000020表示是否有凹槽;
     * 0x00000008表示是否有圆角。
     *
     * @param context Context
     * @return hasNotch
     */
    private static int VIVO_NOTCH = 0x00000020;//是否有刘海
    private static int VIVO_FILLET = 0x00000008;//是否有圆角
    public static boolean hasNotchInVivo(Context context)
    {
        boolean hasNotch = false;
        try
        {
            ClassLoader cl = context.getClassLoader();
            Class FtFeature = cl.loadClass("android.util.FtFeature");
            Method method = FtFeature.getMethod("isFeatureSupport", int.class);
            hasNotch = (boolean) method.invoke(FtFeature, VIVO_NOTCH);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return hasNotch;
    }

    /**
     * HUAWEI
     * com.huawei.android.util.HwNotchSizeUtil
     * public static boolean hasNotchInScreen()
     *
     * @param context Context
     * @return hasNotch
     */
    public static boolean hasNotchInHuawei(Context context)
    {
        boolean hasNotch = false;
        try {
            ClassLoader cl = context.getClassLoader();
            Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
            hasNotch = (boolean) get.invoke(HwNotchSizeUtil);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hasNotch;
    }


    public static boolean hasNotchInSmart(Context context)
    {
        boolean hasNotch = false;
        try
        {
            Class<?> DisplayUtilsSmt = Class.forName("smartisanos.api.DisplayUtilsSmt");
            Method isFeatureSupport = DisplayUtilsSmt.getMethod("isFeatureSupport", int.class);
            hasNotch = (boolean) isFeatureSupport.invoke(DisplayUtilsSmt, 0x00000001);
            return hasNotch;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return hasNotch;
    }

For Android p and above, Google provides an interface to obtain whether the notch screen is supported, and returns the position and size of the notch

 protected static boolean isNotchEnable(Activity activity)
    {
        DisplayCutout displayCutout = activity.getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
        if(displayCutout == null || displayCutout.getBoundingRects() == null || displayCutout.getBoundingRects().size() == 0){
            return false;
        }
        return true;
    }

    @TargetApi(Build.VERSION_CODES.P)
    protected static int[] getNotchInfo(Activity activity)
    {
        int[] notchSize = new int[]{0,0};
        DisplayCutout displayCutout = activity.getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
        List<Rect> boundingRects = displayCutout.getBoundingRects();
        if(boundingRects.size() != 0)
        {
            Rect rect = boundingRects.get(0);
            notchSize[0] = rect.width();
            notchSize[1] = rect.height();
        }
        return notchSize;
    }

In fact, there are many tutorials on the Internet that have already said these, let me talk about the problems I encountered,

First of all, unity needs to check the rendering outside the safe area

Otherwise, there will still be gaps, and then, Android xml needs to be set

 max_aspect represents the screen ratio, 2400/1080 = 2.222;

Encountered a problem when adapting to vivo, he does not open the notch area by default, you need to add a full screen in the oncreate of mainactivity

 @see #LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES 允许内容去延伸进刘海区
private static void SetWindowLayoutInDisplayCutoutMode(Activity act)
	{
		WindowManager.LayoutParams lp = act.getWindow().getAttributes();
		lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
		act.getWindow().setAttributes(lp);
	}

 Generally, the bangs adaptation scheme, in the game, is ui scaling and displacement. There is a good answer here.

How to use unity's ugui to adapt to iPhoneX's notch screen? - Know almost

The strategy he adopts is to move the anchor point closer to the center.

Our game is a horizontal screen, what I do is to check the screen selection, and then the displacement of one side of the hole. Mainly use ScreenOrientation.LandscapeLeft

and the Screen.orientation class

if self._isleftOrientation ~= Screen.orientation then
		self:RefreshUIWithNotch()
		self._isleftOrientation = Screen.orientation
	end

Regarding anchor points or anchor edges, different strategies

If AnchorMin and AnchorMax are equal, it is the anchor point, just AnchorPos.x - notchSizeX

If it is stretch, it will change the size with the screen, anchorPos.x = AnchorPos.x + notchSizeX * (1 - beginPivot.x)

that's probably it

Good guy, the iPhone is no longer satisfied with the screen of Zhang Yuqi, and the water drop screen has begun. Our strategy for the iPhone is to directly write the size of the iPhone platform. The ios platform rawset(self, "_notchSize", Vector2(70, 200))

               

Guess you like

Origin blog.csdn.net/qq_40314347/article/details/125338607