前期准备材料
1、已经导出成功的unity项目,导出的unity项目内部结构见下图
2、新建一个或者使用已有项目
1、第一步,导入unity
打开安卓项目,导入unity的module,找到unity项目中的unityLibrary,选中此module,点击finish后稍等片刻。
2、解决导入module过程中出现的问题
a、在项目的gradle.properties中添加如下代码
unityStreamingAssets=.unity3d, google-services-desktop.json, google-services.json, GoogleService-Info.plist
b、在主module中的build.gradle中添加如下代码
configurations.all {
// 重点大问题:一次性解决support库版本不一致,直接改了所有的依赖项目
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '28.0.0'
}
}
}
}
3、将unity项目中的unityLibrary中新建一个MyUnityPlayer类,继承UnityPlayer类,然后将UN体验项目rebuild,生成的aar文件,复制到安卓项目主module的libs中,并在build.gradle中引用。
public class MyUnityPlayer extends UnityPlayer {
public MyUnityPlayer(Context context) {
super(context);
}
protected void kill() {
}
}
在build.gradle中引用aar:
implementation(name: 'unityLibrary-debug', ext: 'aar')
4、将unityLibrary中jniLibs中的so文件复制到安卓项目的主module中。
如下是unityLibrary中so文件的位置:
如下是主module中位置,如果没有jniLibs文件夹新建即可。
5、在主module中新建页面,并运行程序,成功展示unity效果,然后写上跳转到此页面代码,运行即可看到unity效果。
a、在主module中新建UnityActivity,布局文件中只有一个LinearLayout即可:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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=".unity.SecondActivity">
<LinearLayout
android:id="@+id/unityLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:layout_weight="10"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="104dp">
</LinearLayout>
</LinearLayout>
b、UnityActivity中初始化LinearLayout和MyUnityPlayer,并重写unityplayer生命周期方法
private LinearLayout unityLayout;
private MyUnityPlayer mUnityPlayer;
// 在onCreate中初始化
unityLayout = findViewById(R.id.unityLayout);
getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy
// 创建Unity视图
mUnityPlayer = new MyUnityPlayer(this);
// 添加Unity视图
unityLayout.addView(mUnityPlayer.getView());
mUnityPlayer.requestFocus();
// 以下是生命周期方法---必须要写,否则黑屏
@Override protected void onNewIntent(Intent intent) {
// To support deep linking, we need to make sure that the client can get access to
// the last sent intent. The clients access this through a JNI api that allows them
// to get the intent set on launch. To update that after launch we have to manually
// replace the intent with the one caught here.
super.onNewIntent(intent);
setIntent(intent);
}
// Quit Unity
@Override protected void onDestroy ()
{
mUnityPlayer.quit();
super.onDestroy();
}
// Pause Unity
@Override protected void onPause()
{
super.onPause();
mUnityPlayer.pause();
}
// Resume Unity
@Override protected void onResume()
{
super.onResume();
mUnityPlayer.resume();
}
// Low Memory Unity
@Override public void onLowMemory()
{
super.onLowMemory();
mUnityPlayer.lowMemory();
}
// Trim Memory Unity
@Override public void onTrimMemory(int level)
{
super.onTrimMemory(level);
if (level == TRIM_MEMORY_RUNNING_CRITICAL)
{
mUnityPlayer.lowMemory();
}
}
// This ensures the layout will be correct.
@Override public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
// Notify Unity of the focus change.
@Override public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
// For some reason the multiple keyevent type is not supported by the ndk.
// Force event injection by overriding dispatchKeyEvent().
@Override public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
return mUnityPlayer.injectEvent(event);
return super.dispatchKeyEvent(event);
}
// Pass any events not handled by (unfocused) views straight to UnityPlayer
@Override public boolean onKeyUp(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); }
@Override public boolean onTouchEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); }
/*API12*/ public boolean onGenericMotionEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); }
标签:集成,MyUnityPlayer,项目,unityLibrary,unity,module,gradle,Unity,Android From: https://blog.51cto.com/u_16163453/6512012