unity匯入Android說明新建專案
首先新建一個unity專案和一個Android專案,對應本demo的UnityScene和AndroidProject
Unity Scene將unity配置為Androidfile --> buildingSettings
構建專案勾選export project,building,新建UnityScene資料夾儲存,匯出後文件結構大致如下
引入到Android專案中以下檔案放到Android 專案中對應位置(我僅匯出arm-v7)
unity-classes.jar, armeabi-v7a放到Android project的app/libs目錄下;assets資料夾放到app/src/main資料夾下,與java同級。Android Studio檔案結構如下
gradle中新增(與buildTypes同級)
sourceSets { main { //unity3D jniLibs.srcDirs = ['libs', 'libs-sdk'] }}
defaultConfig中新增
ndk { abiFilters "armeabi-v7a" }
在local.properties中配置ndk(替換你自己的ndk)
ndk.dir=.../Android/sdk/ndk/xxxx
Android架構中應當看到
Android Project建立Fragment建立UnityScene.java
package com.example.androidproject.unity;import com.unity3d.player.UnityPlayer;public class UnityScene { public static UnityPlayer mUnityPlayer; public UnityScene(){ }}
建立一個空的Fragment,新增如下程式碼
private View playerView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment playerView = UnityScene.mUnityPlayer.getView(); //具體引數 跟自己公司Unity開發人員協商 //第一個引數是unity那邊的掛載指令碼名字 //第二個引數是 unity提供的方法名 //第三個引數是 自己要給unity傳的值// UnityScene.mUnityPlayer.UnitySendMessage("Main Camera","Id","1"); return playerView; }
改寫MainActivity
在MainAcivity的onCreate中新增
UnityScene.mUnityPlayer = new UnityPlayer(getApplicationContext());getWindow().setFormat(PixelFormat.RGBX_8888);if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .setReorderingAllowed(true) .add(R.id.fragment_container_view, UnityFragment.class, null) .commit();}
MainAcivity重寫其他方法
@Overrideprotected void onDestroy() { UnityScene.mUnityPlayer.quit(); super.onDestroy();}@Overrideprotected void onPause() { super.onPause(); UnityScene.mUnityPlayer.pause();}@Overrideprotected void onResume() { super.onResume(); UnityScene.mUnityPlayer.resume();}@Overridepublic void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); UnityScene.mUnityPlayer.configurationChanged(newConfig);}@Overridepublic void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); UnityScene.mUnityPlayer.windowFocusChanged(hasFocus);}@Overridepublic boolean dispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_MULTIPLE) return UnityScene.mUnityPlayer.injectEvent(event); return super.dispatchKeyEvent(event);}@Overridepublic boolean onKeyUp(int keyCode, KeyEvent event) { return UnityScene.mUnityPlayer.injectEvent(event);}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) { return UnityScene.mUnityPlayer.injectEvent(event);}@Overridepublic boolean onTouchEvent(MotionEvent event) { return UnityScene.mUnityPlayer.injectEvent(event);}public boolean onGenericMotionEvent(MotionEvent event) { return UnityScene.mUnityPlayer.injectEvent(event);}
gradle中增加
implementation "androidx.fragment:fragment:1.2.1"
activity_main.xml如下
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container_view" android:layout_width="match_parent" android:layout_height="500dp" app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
string.xml中新增
<string name="game_view_content_description">Game View</string>
完成
執行專案即可看到unity場景巢狀在fragment中
最新評論