Unity Androidはノッチ画面、ウォータードロップ画面、異性画面と互換性があります

携帯電話メーカーはトレンドセッターの集団であり、毎回、さまざまな奇抜な画面で新しいトリックを仕掛けてきますが、プレイヤーの没入感を高めるためには、異性の画面に合わせて開発する必要があります。ゲーム。

一般的には Android 方式が採用されていますが、実際にはインターネット上に多くの解決策が存在しており、主流となっているのは Google の公式インターフェースである穴掘り画面です。

1 つ目は、Android p バージョン (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;
    }

一般に、携帯電話メーカーは適応ソリューションを提供します。

<メタデータ
 android:name="notch.config"
 android:value="portrait|landscape"/>

このインターフェースを使用してください。アプリケーションの下に耳の領域を使用するかどうかのメタデータを追加します

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;
    }

Android p 以降の場合、Google はノッチ画面がサポートされているかどうかを取得し、ノッチの位置とサイズを返すインターフェースを提供します。

 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;
    }

実際、インターネット上にはすでにこれらのことを述べたチュートリアルがたくさんあります。私が遭遇した問題について話しましょう。

まず、Unity は安全領域外のレンダリングをチェックする必要があります。

そうしないと、ギャップが残るため、Android XML を設定する必要があります。

 max_aspect は画面比率を表し、2400/1080 = 2.222;

vivo に適応するときに問題が発生しました。デフォルトではノッチ領域が開きません。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);
	}

 一般に、ゲームにおける前髪の適応スキームは、UI のスケーリングとディスプレイスメントです。ここに良い答えがあります。

Unityのuguiを使ってiPhoneXのノッチ画面に適応させるにはどうすればいいですか?- ほぼ知っています

彼が採用した戦略は、アンカーポイントを中心に近づけることです。

私たちのゲームは横画面です。私が行うのは、画面の選択を確認し、穴の片側の変位を確認することです。主に ScreenOrientation.LandscapeLeft を使用します。

Screen.orientation クラス

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

アンカーポイントやアンカーエッジに関しては、戦略が異なります

AnchorMin と AnchorMax が等しい場合、それはアンカー ポイントであり、単に AnchorPos.x - notchSizeX です。

ストレッチの場合は画面に合わせてサイズを変更します、anchorPos.x = AnchorPos.x + notchSizeX * (1 - beginPivot.x)

おそらくそれです

いいやつです。iPhone は張玉琦の画面に​​満足できなくなり、ウォーター ドロップ スクリーンが始まりました。iPhone に対する私たちの戦略は、iPhone プラットフォームのサイズを直接書き込むことです。ios プラットフォームの rawset(self, "_notchSize" 、ベクトル2(70, 200))

               

おすすめ

転載: blog.csdn.net/qq_40314347/article/details/125338607