案例测试环境:
Unity: 2020.1.7
Android Studio:4.1
1.创建工程
2.切换为 Android 平台, 勾选 Export Project ,然后 Player Settings 设置,尤其是包名,设置好后, 就可以 Export 导出工程,具体如下图
3.导出后的 Unity 工程,具体如下图
4.打开 Android Studio ,新建一个工程,新建一个 Empty Activity,具体如下图,注意包名以导出的 Unity 工程的包名一致
5.把 Unity导出的工程的文件,对应添加到新建的 Android 工程,包括 unity-class.jar,assets文件夹,和 jniLibs 文件夹,具体如下图
注意:将unity-class.jar添加进去记得右键unity-class.jar 选择Add as Library
继续导入其它资源
6.MainActivity.java 中添加一个按钮点击事件,新建TestUnityActivity.java,用于显示UnityActivity界面(注意:继承 UnityPlayerActivity),并监听返回键事件返回到MainActivity; 布局文件等 具体如下图
添加返回主界面的方法
给按钮绑定点击事件
将Unity界面注册进去
运行结果
注意:打开闪退解决方案
注意:Android无法找到Unity: Failed to load 'libmain.so'
(修改配置文件是app目录下的build.gradle)
7.具体代码如下
MainActivity.java
package com.hy.testdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.unity3d.player.UnityPlayer;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ButtonClick(View view)
{
Intent intent=new Intent(this,TestUnityActivity.class);
startActivity(intent);
}
}
TestUnityActivity.java
package com.hy.testdemo;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
public class TestUnityActivity extends UnityPlayerActivity {
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
}
public boolean onKeyDown(int i, KeyEvent keyEvent)
{
if(i==KeyEvent.KEYCODE_BACK)
{
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
finish();
}
return super.onKeyDown(i,keyEvent);
}
}
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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="159dp"
android:layout_marginTop="341dp"
android:layout_marginEnd="159dp"
android:layout_marginBottom="342dp"
android:onClick="ButtonClick"
android:text="测试"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hy.testdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Testdemo">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TestUnityActivity"></activity>
</application>
</manifest>
strings.xml
<resources>
<string name="app_name">testdemo</string>
<string name="game_view_content_description">Game view</string>
</resources>
build.gradle
defaultConfig {
ndk {
abiFilters 'armeabi-v7a', 'x86'
}
}