將unity3d項目嵌入到Android App中使用

建立一個新的AndroidStudio app項目。android

 

1.添加庫文件:
拷貝unity安裝目錄下的庫文件:Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\classes.jar
到安卓項目目錄下:app\libs\unitylibs.jarapp

2.拷貝unity安裝目錄下的全部文件夾:Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Libs
到安卓項目目錄下:app\src\main\jniLibside

3.拷貝資源
將unity項目的編譯好的apk包解壓後,把assets文件夾拷貝到安卓項目app\src\main中ui

4.AndroidManifest.xml添加UnityActivity和功能屬性:this

<activity android:name=".UnityActivity" android:process="e.unitry3d" android:screenOrientation="landscape" 
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density">
</activity>

<uses-feature android:glEsVersion="0x00020000" />
<uses-feature android:name="android.hardware.vulkan" android:required="false" />
<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" />

 

 

5.添加一個新Activity:spa

 
 
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.unity3d.player.UnityPlayer;

public class UnityActivity extends AppCompatActivity {
private LinearLayout mLlUnityContainer;
Button btnBack;
protected UnityPlayer mUnityPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(1);
super.onCreate(savedInstanceState);
this.getWindow().setFormat(2);
setContentView(R.layout.activity_unity);
mLlUnityContainer = findViewById(R.id.unity_container);
btnBack = findViewById(R.id.btn_back);

this.mUnityPlayer = new UnityPlayer(this);
this.mUnityPlayer.requestFocus();
mLlUnityContainer.addView(mUnityPlayer.getView());

btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}

@Override
protected void onNewIntent(Intent intent) {
this.setIntent(intent);
}

@Override
protected void onDestroy() {
this.mUnityPlayer.quit();
super.onDestroy();
}

@Override
protected void onPause() {
super.onPause();
this.mUnityPlayer.pause();
}

@Override
protected void onResume() {
super.onResume();
this.mUnityPlayer.resume();
}

@Override
public void onLowMemory() {
super.onLowMemory();
this.mUnityPlayer.lowMemory();
}

@Override
public void onTrimMemory(int var1) {
super.onTrimMemory(var1);
if (var1 == 15) {
this.mUnityPlayer.lowMemory();
}

}

@Override
public void onConfigurationChanged(Configuration var1) {
super.onConfigurationChanged(var1);
this.mUnityPlayer.configurationChanged(var1);
}

@Override
public void onWindowFocusChanged(boolean var1) {
super.onWindowFocusChanged(var1);
this.mUnityPlayer.windowFocusChanged(var1);
}

@Override
public boolean dispatchKeyEvent(KeyEvent var1) {
return var1.getAction() == 2 ? this.mUnityPlayer.injectEvent(var1) : super.dispatchKeyEvent(var1);
}

@Override
public boolean onKeyUp(int var1, KeyEvent var2) {
return this.mUnityPlayer.injectEvent(var2);
}

@Override
public boolean onKeyDown(int var1, KeyEvent var2) {
return this.mUnityPlayer.injectEvent(var2);
}

@Override
public boolean onTouchEvent(MotionEvent var1) {
return this.mUnityPlayer.injectEvent(var1);
}

@Override
public boolean onGenericMotionEvent(MotionEvent var1) {
return this.mUnityPlayer.injectEvent(var1);
}

}
 
activity_unity.xml代碼以下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_back"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="back"
        android:textSize="16sp"/>

    <LinearLayout
        android:id="@+id/unity_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    </LinearLayout>
</LinearLayout>

  轉載請註明出處,from博客園HemJohn3d

相關文章
相關標籤/搜索