unity安卓如何适配全面屏?

更新:升级Unity新版本后,在Player Setting里将该Aspect Ratio Mode改成Custom选项即可,Up To设置为2.2,新版本应该默认是这样的。

-------------------------------------

使用unity打包Android应用安装到全面屏手机上,会发现应用两边会有黑条,并且获取的Screen.width和Screen.height都是非完全的屏幕尺寸。这是因为google android为了向下兼容。所以需要在AndroidManifest.xml中开启选项即可。

Unity打包后正常的Manifest文件如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cottongame.Isoland2" android:versionName="1.0.0" android:versionCode="1" android:installLocation="preferExternal">
  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
  <application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false" android:isGame="true">
    <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:screenOrientation="sensorLandscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
      </intent-filter>
      <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    </activity>
  </application>
  <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23" />
  <uses-feature android:glEsVersion="0x00020000" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
  <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
  <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
</manifest>

适配全面屏后如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cottongame.Isoland2" android:versionName="1.0.0" android:versionCode="1" android:installLocation="preferExternal">
  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
  <application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false" android:isGame="true">
    <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:screenOrientation="sensorLandscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
      </intent-filter>
      <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    </activity>
	<meta-data android:name="android.max_aspect" android:value="2.2" />
  </application>
  <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23" />
  <uses-feature android:glEsVersion="0x00020000" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
  <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
  <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
</manifest>

 就是这句话起了作用:

<meta-data android:name="android.max_aspect" android:value="2.2" />

 那么Unity打包Android程序会发现没有这个Manfiest文件怎么办呢?其实很简单。

  • 在unity项目内的Temp文件夹找到unity自动生成的安卓文件(ps:要先发布apk出来才有)

  • 在项目路径Temp/StagingArea会看到AndroidManfiest.xml文件
  • 将AndroidManfiest.xml复制到Assets\Plugins\Android目录下
  • 打开xml文件,加入上面的那句话即可

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
为了在Unity适配挖孔,可以按照以下步骤进行操作: 1.首先,需要检查方向。如果游戏是横的,可以使用ScreenOrientation.LandscapeLeft。 2.然后,需要确定幕上的挖孔位置。可以使用DeviceScreen类来获取挖孔的位置和大小。 3.接下来,需要根据挖孔的位置和大小来调整UI元素的位置和大小。可以使用RectTransform组件来调整UI元素的位置和大小。 4.如果幕是stretch的,那么UI元素的位置和大小会随着幕的改变而改变。可以使用anchorPos.x = AnchorPos.x + notchSizeX * (1 - beginPivot.x)来调整UI元素的位置。 5.最后,需要在代码中处理幕旋转和大小改变的事件,以确保UI元素始终正确地适应幕。 以下是一个示例代码,用于在Unity适配挖孔: ```csharp using UnityEngine; public class NotchScreenAdapter : MonoBehaviour { public RectTransform rectTransform; public ScreenOrientation screenOrientation; private void Start() { // 检查幕方向 Screen.orientation = screenOrientation; // 获取挖孔的位置和大小 DeviceScreen deviceScreen = new DeviceScreen(); Rect safeArea = deviceScreen.safeArea; Vector2 notchSize = new Vector2(safeArea.width, safeArea.height); // 调整UI元素的位置和大小 Vector2 anchorPos = rectTransform.anchoredPosition; Vector2 beginPivot = rectTransform.pivot; anchorPos.x = anchorPos.x + notchSize.x * (1 - beginPivot.x); rectTransform.anchoredPosition = anchorPos; rectTransform.sizeDelta = new Vector2(rectTransform.sizeDelta.x - notchSize.x, rectTransform.sizeDelta.y); } private void Update() { // 处理幕旋转和大小改变的事件 DeviceScreen deviceScreen = new DeviceScreen(); if (deviceScreen.safeArea.width != Screen.width || deviceScreen.safeArea.height != Screen.height) { Start(); } } } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼蛋-Felix

如果对你有用,可以请我喝杯可乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值