1、集成方法
1、Unity导出Android项目之后,将以下几个文件拷贝到AndroidStudio创建的新项目中
libs下的unity-classes.jar
src/main/assets
src/main/包路径/UnityPlayerActivity.java
src/main/jniLibs
src/main/res
src/main/AndroidManifest.xml
2、在MainActivity中使用Intent跳转到UnityPlayerActivity出现闪退,解决方法看需要注意的问题:
需要注意的问题如下几点(最后一条最重要):
1)、修改build.gradle配置文件:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
<!-- 引入unity-classes.jar -->
api fileTree(dir: 'libs', include: 'unity-classes.jar')
}
2)、在AndroidManifest.xml中添加Unity游戏场景的Activity
<activity android:label="@string/app_name" android:screenOrientation="fullSensor" android:launchMode="singleTask" android:process=".unityplayer" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density" android:hardwareAccelerated="false" android:name="com.vip.test.UnityPlayerActivity">
<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>
3)、在AndroidManifest.xml文件中application的主题:
将:android:theme="@style/UnityThemeSelector"
修改为:android:theme="@style/AppTheme"
4、如果是使用AndroidStudio新建的项目,则把res文件夹下的所有文件删除(布局文件需要留着),然后拷贝Unity3d导出的Android项目的res文件夹下的所有文件
在Unity中使用Application.Quit退出时整个App都退出了,解决方法:
在AndroidManifest.xml文件中的UnityActivity的activity上配置
<!-- 给指定的Activity单独一个线程 -->
android:process=".unityplayer"
5)、加入Vuforia之后,出现的闪退情况使用以下方法解决
<string name="game_view_content_description">Game view</string>
ndk {
abiFilters 'armeabi-v7a', 'x86'
}
6)、配置的EasyAR包名不一致
2、Unity导出Android项目打包Apk加载场景慢
在主项目的build.gradle中配置,可以在unity到处的项目的launcher项目的build.gradle中查看
aaptOptions {
noCompress = ['.unity3d', '.ress', '.resource', '.obb']
}
另外在使用Application.Quit退出Unity场景后,再次进入需要重新加载,解决方法:
待解决