Android端集成unityLibrary
- 第一步引入 unityLibrary包到專案中
- 第二步在settings.gradle里面添加
- 第三步 專案級build.gradle里面添加
- 第四步 修改unityLibrary里面的 AndroidManifest.xml 檔案
- 第五步在unityLibrary新建一個 MyUnityPlayer.class 繼承UnityPlayer
- 第六步在UnityPlayerActivity 中 寫一個 ReturnAPP方法 unity點擊回傳退出當前unity界面回傳app主界面
- 第七步app主專案參考unityLibrary庫
- 第八步 跳轉到unity界面
- 解決一些沖突問題
第一步引入 unityLibrary包到專案中

第二步在settings.gradle里面添加
project(':unityLibrary').projectDir=new File('unityLibrary')
第三步 專案級build.gradle里面添加
flatDir {
dirs "${project(':unityLibrary').projectDir}/libs"
}

第四步 修改unityLibrary里面的 AndroidManifest.xml 檔案
//洗掉
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.notch_support"
android:value="true" />
//這樣就不會app啟動時先啟動unity程式
//在 <activity> 添加 使unity成為獨立的行程,這樣退出unity時不會閃退
android:launchMode="singleTask"
android:process="e.unity3d"

第五步在unityLibrary新建一個 MyUnityPlayer.class 繼承UnityPlayer
public class MyUnityPlayer extends UnityPlayer {
public MyUnityPlayer(Context context) {
super(context);
}
public MyUnityPlayer(Context context, IUnityPlayerLifecycleEvents iUnityPlayerLifecycleEvents) {
super(context, iUnityPlayerLifecycleEvents);
}
public void kill() {
}
}
第六步在UnityPlayerActivity 中 寫一個 ReturnAPP方法 unity點擊回傳退出當前unity界面回傳app主界面
/*
Android 退出App
*/
public void ReturnAPP(){
finish();
}
第七步app主專案參考unityLibrary庫
在app底下build.gradle中dependencies添加
implementation project(path: ':unityLibrary')
第八步 跳轉到unity界面
Intent intent = new Intent(MainActivity.this, UnityPlayerActivity.class);
startActivity(intent);
解決一些沖突問題
在app底下AndroidManifest.xml application中添加
tools:replace="android:theme"
解決合并AndroidManifest.xml檔案AppTheme沖突問題
在app底下strings.xml檔案中添加 解決打開unity界面找不到資源檔案的問題
<string name="game_view_content_description"></string>
在app底下 build.gradle 中設定支持的SO庫架構 解決unityLibrary庫 .so檔案兼容問題
ndk {
abiFilters 'armeabi-v7a'
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/230323.html
標籤:其他
