Appearance
Unity接入-Android平台配置
WARNING
在完成Unity层API调用之后, 如果要出Android平台的apk包,还需要做一些额外的配置
依赖配置
将Unity编译设置中,当前的平台切换到Android平台。之后在发布设置中,勾选自定义main gradle template。勾选后,会在Assets/Plugins/Android/下面,生成mainTemplate.gradle文件。
打开Assets/Plugins/Android/mainTemaplate.gradle, 在dependencies添加如下依赖:
java
// SDK UI组件依赖
implementation 'androidx.fragment:fragment:1.2.0'
// 微信支付
implementation 'com.tencent.mm.opensdk:wechat-sdk-android:+'
// 支付宝支付
implementation 'com.alipay.sdk:alipaysdk-android:+@aar'
// 阿里云本机号码一键登录/QQ登录 需要
implementation 'androidx.appcompat:appcompat:1.3.1'
// Tap登录需要
implementation 'com.taptap.sdk:tap-core:4.5.9'
implementation 'com.taptap.sdk:tap-login:4.5.9'
implementation 'com.taptap.sdk:tap-update:4.5.9'
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
implementation 'androidx.core:core:1.3.1'
implementation 'com.google.android.flexbox:flexbox:3.0.0'
配置参数
打开Assets/Scripts/SDK/SDKInterfaceAndroid.cs,
将Init方法中,initParams.appID、initParams.appKey和initParams.orientation设置为当前游戏的参数(在SDK后台-》游戏控制台-》基础配置中查看)
csharp
UGInitParams initParams = new UGInitParams();
initParams.appID = "1"; //SDK后台该游戏的appID
initParams.appKey = "1111111"; //SDK后台该游戏的appKey
initParams.orientation = UGInitParams.ORIENTATION_LANDSCAPE; //横竖屏, 横屏:UGInitParams.ORIENTATION_LANDSCAPE; 竖屏:UGInitParams.ORIENTATION_PORTRAIT
启动类配置
打开Assets/Plugins/Android/AndroidManifest.xml, 默认启动Activity是:com.xxx.sdk.SDKUnityActivity,该Activity继承了com.unity3d.player.UnityPlayerActivity。
xml
<activity
android:name="com.xxx.sdk.SDKUnityActivity"
android:screenOrientation="landscape"
android:launchMode="singleTask"
android:configChanges="orientation|keyboardHidden|screenSize"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
该类是Android原生层和Unity层API交互的桥梁,对应源码在UGSDK工程/sdk-unity-bridge模块, 如果游戏有自己的API需要扩展,可以在这里添加供Unity层调用的接口。
修改代码完成后执行 ./gradlew :sdk-unity-bridge:generateJar
进行编译,编译成功后, 会在其build/libs下生成sdk-unity-bridge.jar,将其替换到Unity工程Assets/Plugins/Android/libs目录下。
Application配置
打开Assets/Plugins/Android/AndroidManifest.xml, 默认配置的Application是:com.game.sdk.app.UGApplication。
xml
<application android:name="com.game.sdk.app.UGApplication" android:usesCleartextTraffic="true">
...
</application>
如果游戏有自己的Application, 那可以按Android接入文档中实现Application
模块的说明进行调整:Android接入配置
其他配置
如果开启了QQ登录,还需要配置QQ登录相关的AndroidManifest.xml:
xml
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="behind"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name="com.tencent.tauth.AuthActivity"
android:launchMode="singleTask"
android:noHistory="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!--注意,这里的${qqAppID}要换成该游戏自己的QQ AppID-->
<data android:scheme="tencent${qqAppID}" />
</intent-filter>
</activity>
导出APK
上述配置完成后,即可导出apk或AS工程, 在导出配置中, 需要注意包名、版本信息的配置, Target API Level请选30(Android 11.0)