Android Studio项目集成Unity详细教程
前言
我们知道Unity可以直接打包APK,也可以导出AS或Eclipse工程,我的应用场景是已经有个Android Studio大项目了,Unity所做的功能属于其一部分,所以选择将Unity导出AS工程,为了方便主项目调用,还将Unity部分打包成aar 。
环境:Android Studio 3.0 + Unity 2017
1.将统一工程导出AS工程
如下图红框处,Build System选择Gradle,Export Project勾选,按图中箭头把包名填好,不能使用Unity默认的,否则导出不了,点击导出导出。
导出结果:
2.AS导入上一步的工程,选择导入项目(Gradle,Eclipse ADT等)
导入多半是会报错的,一般错误就是gradle这个版本没选对,按自己的情况选好就行了,比如我的是3.0.0。
3.继承UnityPlayer
此时我们来看看这个工程结构,就一个界面UnityPlayerActivity,因为做统一游戏的话也不需要显示其它原生界面,网上有些教程就是用继承UnityPlayerActivity的方法来显示统一,然而这样做不够灵活。查看UnityPlayerActivity我们可以发现真正关键的是UnityPlayer所以仿照UnityPlayerActivity直接操作UnityPlayer不就好了么,但是......但是这里有个坑,我直接用UnityPlayer正确显示了统一界面,可是在退出含统一界面的活动时,整个应用退出了,难道有错误导致整个应用崩了经过查找,发现原来在UnityPlayer源码中?
这个kill方法在活动完成时会被调用,然而它竟然给我把当前应用进程杀死了!没办法,自己写个人MyUnityPlayer继承它,并重写父类的杀方法。
<span style="color:#2f2f2f"><span style="color:#abb2bf"><code><span style="color:#c678dd">package</span> com.unity.demo;
<span style="color:#c678dd">import</span> android.content.Context;
<span style="color:#c678dd">import</span> com.unity3d.player.UnityPlayer;
<span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> <span style="color:#e6c07b">MyUnityPlayer</span> <span style="color:#c678dd">extends</span> <span style="color:#e6c07b">UnityPlayer</span> {
<span style="color:#c678dd">public</span> <span style="color:#61aeee">MyUnityPlayer</span>(Context context) {
<span style="color:#c678dd">super</span>(context);
}
<span style="color:#61aeee">@Override</span>
<span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">kill</span>() {
<span style="color:#929292">//不要杀死当前应用进程</span>
}
}
</code></span></span>
4.导出AAR包
百度“Android Studio导出aar包”,发现还要创建模块,有那么麻烦吗?直接改build.gradle就行了。如下图,apply plugin:'com.android.application'改为apply plugin:'com.android .library',注释掉applicationId,然后Rebuild Project。完成后在工程的构建 - 输出 - aar文件夹下就可以看到aar包了。
5.在主AS工程中使用unity aar包
这里我就用一个简单的Demo来演示,把aar文件放入新工程的app - libs文件夹下,修改app的build.gradle:
演示项目
演示项目用两个界面呈现,主界面和第二界面,统一内容使用的FrameLayout或LinearLayout中显示,代码如下:
MainActivity.java
<span style="color:#2f2f2f"><span style="color:#abb2bf"><code><span style="color:#c678dd">package</span> com.my.unityapp;
<span style="color:#c678dd">import</span> android.app.Activity;
<span style="color:#c678dd">import</span> android.content.Intent;
<span style="color:#c678dd">import</span> android.os.Bundle;
<span style="color:#c678dd">import</span> android.view.View;
<span style="color:#c678dd">import</span> android.widget.Button;
<span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> <span style="color:#e6c07b">MainActivity</span> <span style="color:#c678dd">extends</span> <span style="color:#e6c07b">Activity</span> {
<span style="color:#c678dd">private</span> Button firstButton;
<span style="color:#61aeee">@Override</span>
<span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onCreate</span>(Bundle savedInstanceState) {
<span style="color:#c678dd">super</span>.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstButton = findViewById(R.id.firstButton);
firstButton.setOnClickListener(<span style="color:#c678dd">new</span> View.OnClickListener() {
<span style="color:#61aeee">@Override</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onClick</span>(View view) {
Intent intent = <span style="color:#c678dd">new</span> Intent(MainActivity.<span style="color:#c678dd">this</span>, SecondActivity.class);
startActivity(intent);
}
});
}
}
</code></span></span>
activity_main.xml中
<span style="color:#2f2f2f"><span style="color:#abb2bf"><code><span style="color:#61aeee"><?</span>xml version=<span style="color:#98c379">"1.0"</span> encoding=<span style="color:#98c379">"utf-8"</span><span style="color:#61aeee">?></span>
<<span style="color:#e06c75">android.support.constraint.ConstraintLayout</span>
<span style="color:#d19a66">xmlns:android</span>=<span style="color:#98c379">"http://schemas.android.com/apk/res/android"</span>
<span style="color:#d19a66">xmlns:tools</span>=<span style="color:#98c379">"http://schemas.android.com/tools"</span>
<span style="color:#d19a66">xmlns:app</span>=<span style="color:#98c379">"http://schemas.android.com/apk/res-auto"</span>
<span style="color:#d19a66">android:layout_width</span>=<span style="color:#98c379">"match_parent"</span>
<span style="color:#d19a66">android:layout_height</span>=<span style="color:#98c379">"match_parent"</span>
<span style="color:#d19a66">tools:context</span>=<span style="color:#98c379">"com.my.unityapp.MainActivity"</span>>
<<span style="color:#e06c75">TextView</span>
<span style="color:#d19a66">android:id</span>=<span style="color:#98c379">"@+id/textView"</span>
<span style="color:#d19a66">android:layout_width</span>=<span style="color:#98c379">"wrap_content"</span>
<span style="color:#d19a66">android:layout_height</span>=<span style="color:#98c379">"wrap_content"</span>
<span style="color:#d19a66">android:text</span>=<span style="color:#98c379">"First Activity"</span>
<span style="color:#d19a66">app:layout_constraintBottom_toBottomOf</span>=<span style="color:#98c379">"parent"</span>
<span style="color:#d19a66">app:layout_constraintLeft_toLeftOf</span>=<span style="color:#98c379">"parent"</span>
<span style="color:#d19a66">app:layout_constraintRight_toRightOf</span>=<span style="color:#98c379">"parent"</span>
<span style="color:#d19a66">app:layout_constraintTop_toTopOf</span>=<span style="color:#98c379">"parent"</span>
<span style="color:#d19a66">app:layout_constraintVertical_bias</span>=<span style="color:#98c379">"0.032"</span> />
<<span style="color:#e06c75">Button</span>
<span style="color:#d19a66">android:id</span>=<span style="color:#98c379">"@+id/firstButton"</span>
<span style="color:#d19a66">android:layout_width</span>=<span style="color:#98c379">"wrap_content"</span>
<span style="color:#d19a66">android:layout_height</span>=<span style="color:#98c379">"wrap_content"</span>
<span style="color:#d19a66">android:layout_marginBottom</span>=<span style="color:#98c379">"8dp"</span>
<span style="color:#d19a66">android:layout_marginEnd</span>=<span style="color:#98c379">"8dp"</span>
<span style="color:#d19a66">android:layout_marginStart</span>=<span style="color:#98c379">"8dp"</span>
<span style="color:#d19a66">android:layout_marginTop</span>=<span style="color:#98c379">"8dp"</span>
<span style="color:#d19a66">android:text</span>=<span style="color:#98c379">"跳转到Unity界面"</span>
<span style="color:#d19a66">app:layout_constraintBottom_toBottomOf</span>=<span style="color:#98c379">"parent"</span>
<span style="color:#d19a66">app:layout_constraintEnd_toEndOf</span>=<span style="color:#98c379">"parent"</span>
<span style="color:#d19a66">app:layout_constraintStart_toStartOf</span>=<span style="color:#98c379">"parent"</span>
<span style="color:#d19a66">app:layout_constraintTop_toBottomOf</span>=<span style="color:#98c379">"@+id/textView"</span> />
</<span style="color:#e06c75">android.support.constraint.ConstraintLayout</span>>
</code></span></span>
SecondActivity.java基本就是仿照UnityPlayerActivity写的
<span style="color:#2f2f2f"><span style="color:#abb2bf"><code><span style="color:#c678dd">package</span> com.my.unityapp;
<span style="color:#c678dd">import</span> android.app.Activity;
<span style="color:#c678dd">import</span> android.content.Intent;
<span style="color:#c678dd">import</span> android.content.res.Configuration;
<span style="color:#c678dd">import</span> android.graphics.PixelFormat;
<span style="color:#c678dd">import</span> android.os.Bundle;
<span style="color:#c678dd">import</span> android.view.KeyEvent;
<span style="color:#c678dd">import</span> android.view.MotionEvent;
<span style="color:#c678dd">import</span> android.view.View;
<span style="color:#c678dd">import</span> android.widget.Button;
<span style="color:#c678dd">import</span> android.widget.LinearLayout;
<span style="color:#c678dd">import</span> com.unity.demo.MyUnityPlayer;
<span style="color:#c678dd">import</span> com.unity3d.player.UnityPlayer;
<span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> <span style="color:#e6c07b">SecondActivity</span> <span style="color:#c678dd">extends</span> <span style="color:#e6c07b">Activity</span> {
<span style="color:#c678dd">private</span> LinearLayout unityLayout;
<span style="color:#c678dd">private</span> Button secondButton;
<span style="color:#c678dd">private</span> MyUnityPlayer mUnityPlayer;
<span style="color:#61aeee">@Override</span>
<span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onCreate</span>(Bundle savedInstanceState) {
<span style="color:#c678dd">super</span>.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
unityLayout = findViewById(R.id.unityLayout);
secondButton = findViewById(R.id.secondButton);
secondButton.setOnClickListener(<span style="color:#c678dd">new</span> View.OnClickListener() {
<span style="color:#61aeee">@Override</span>
<span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onClick</span>(View view) {
SecondActivity.<span style="color:#c678dd">this</span>.finish();
}
});
getWindow().setFormat(PixelFormat.RGBX_8888); <span style="color:#929292">// <--- This makes xperia play happy</span>
<span style="color:#929292">// 创建Unity视图</span>
mUnityPlayer = <span style="color:#c678dd">new</span> MyUnityPlayer(<span style="color:#c678dd">this</span>);
<span style="color:#929292">// 添加Unity视图</span>
unityLayout.addView(mUnityPlayer.getView());
mUnityPlayer.requestFocus();
}
<span style="color:#61aeee">@Override</span> <span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onNewIntent</span>(Intent intent)
{
<span style="color:#929292">// To support deep linking, we need to make sure that the client can get access to</span>
<span style="color:#929292">// the last sent intent. The clients access this through a JNI api that allows them</span>
<span style="color:#929292">// to get the intent set on launch. To update that after launch we have to manually</span>
<span style="color:#929292">// replace the intent with the one caught here.</span>
setIntent(intent);
}
<span style="color:#929292">// Quit Unity</span>
<span style="color:#61aeee">@Override</span> <span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onDestroy</span> ()
{
mUnityPlayer.quit();
<span style="color:#c678dd">super</span>.onDestroy();
}
<span style="color:#929292">// Pause Unity</span>
<span style="color:#61aeee">@Override</span> <span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onPause</span>()
{
<span style="color:#c678dd">super</span>.onPause();
mUnityPlayer.pause();
}
<span style="color:#929292">// Resume Unity</span>
<span style="color:#61aeee">@Override</span> <span style="color:#c678dd">protected</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onResume</span>()
{
<span style="color:#c678dd">super</span>.onResume();
mUnityPlayer.resume();
}
<span style="color:#929292">// Low Memory Unity</span>
<span style="color:#61aeee">@Override</span> <span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onLowMemory</span>()
{
<span style="color:#c678dd">super</span>.onLowMemory();
mUnityPlayer.lowMemory();
}
<span style="color:#929292">// Trim Memory Unity</span>
<span style="color:#61aeee">@Override</span> <span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onTrimMemory</span>(<span style="color:#c678dd">int</span> level)
{
<span style="color:#c678dd">super</span>.onTrimMemory(level);
<span style="color:#c678dd">if</span> (level == TRIM_MEMORY_RUNNING_CRITICAL)
{
mUnityPlayer.lowMemory();
}
}
<span style="color:#929292">// This ensures the layout will be correct.</span>
<span style="color:#61aeee">@Override</span> <span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onConfigurationChanged</span>(Configuration newConfig)
{
<span style="color:#c678dd">super</span>.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
<span style="color:#929292">// Notify Unity of the focus change.</span>
<span style="color:#61aeee">@Override</span> <span style="color:#c678dd">public</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">onWindowFocusChanged</span>(<span style="color:#c678dd">boolean</span> hasFocus)
{
<span style="color:#c678dd">super</span>.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
<span style="color:#929292">// For some reason the multiple keyevent type is not supported by the ndk.</span>
<span style="color:#929292">// Force event injection by overriding dispatchKeyEvent().</span>
<span style="color:#61aeee">@Override</span> <span style="color:#c678dd">public</span> <span style="color:#c678dd">boolean</span> <span style="color:#61aeee">dispatchKeyEvent</span>(KeyEvent event)
{
<span style="color:#c678dd">if</span> (event.getAction() == KeyEvent.ACTION_MULTIPLE)
<span style="color:#c678dd">return</span> mUnityPlayer.injectEvent(event);
<span style="color:#c678dd">return</span> <span style="color:#c678dd">super</span>.dispatchKeyEvent(event);
}
<span style="color:#929292">// Pass any events not handled by (unfocused) views straight to UnityPlayer</span>
<span style="color:#61aeee">@Override</span> <span style="color:#c678dd">public</span> <span style="color:#c678dd">boolean</span> <span style="color:#61aeee">onKeyUp</span>(<span style="color:#c678dd">int</span> keyCode, KeyEvent event) { <span style="color:#c678dd">return</span> mUnityPlayer.injectEvent(event); }
<span style="color:#61aeee">@Override</span> <span style="color:#c678dd">public</span> <span style="color:#c678dd">boolean</span> <span style="color:#61aeee">onKeyDown</span>(<span style="color:#c678dd">int</span> keyCode, KeyEvent event) { <span style="color:#c678dd">return</span> mUnityPlayer.injectEvent(event); }
<span style="color:#61aeee">@Override</span> <span style="color:#c678dd">public</span> <span style="color:#c678dd">boolean</span> <span style="color:#61aeee">onTouchEvent</span>(MotionEvent event) { <span style="color:#c678dd">return</span> mUnityPlayer.injectEvent(event); }
<span style="color:#929292">/*API12*/</span> <span style="color:#c678dd">public</span> <span style="color:#c678dd">boolean</span> <span style="color:#61aeee">onGenericMotionEvent</span>(MotionEvent event) { <span style="color:#c678dd">return</span> mUnityPlayer.injectEvent(event); }
}
</code></span></span>
activity_second.xml
<span style="color:#2f2f2f"><span style="color:#abb2bf"><code><span style="color:#61aeee"><?</span>xml version=<span style="color:#98c379">"1.0"</span> encoding=<span style="color:#98c379">"utf-8"</span><span style="color:#61aeee">?></span>
<<span style="color:#e06c75">LinearLayout</span> <span style="color:#d19a66">xmlns:android</span>=<span style="color:#98c379">"http://schemas.android.com/apk/res/android"</span>
<span style="color:#d19a66">xmlns:app</span>=<span style="color:#98c379">"http://schemas.android.com/apk/res-auto"</span>
<span style="color:#d19a66">xmlns:tools</span>=<span style="color:#98c379">"http://schemas.android.com/tools"</span>
<span style="color:#d19a66">android:layout_width</span>=<span style="color:#98c379">"match_parent"</span>
<span style="color:#d19a66">android:layout_height</span>=<span style="color:#98c379">"match_parent"</span>
<span style="color:#d19a66">android:orientation</span>=<span style="color:#98c379">"vertical"</span>
<span style="color:#d19a66">android:padding</span>=<span style="color:#98c379">"5dp"</span>
<span style="color:#d19a66">tools:context</span>=<span style="color:#98c379">"com.my.unityapp.SecondActivity"</span>>
<<span style="color:#e06c75">LinearLayout</span>
<span style="color:#d19a66">android:id</span>=<span style="color:#98c379">"@+id/unityLayout"</span>
<span style="color:#d19a66">android:layout_width</span>=<span style="color:#98c379">"match_parent"</span>
<span style="color:#d19a66">android:layout_height</span>=<span style="color:#98c379">"match_parent"</span>
<span style="color:#d19a66">android:layout_marginTop</span>=<span style="color:#98c379">"8dp"</span>
<span style="color:#d19a66">android:layout_weight</span>=<span style="color:#98c379">"10"</span>
<span style="color:#d19a66">android:orientation</span>=<span style="color:#98c379">"horizontal"</span>
<span style="color:#d19a66">app:layout_constraintTop_toTopOf</span>=<span style="color:#98c379">"parent"</span>
<span style="color:#d19a66">tools:layout_editor_absoluteX</span>=<span style="color:#98c379">"104dp"</span>>
</<span style="color:#e06c75">LinearLayout</span>>
<<span style="color:#e06c75">Button</span>
<span style="color:#d19a66">android:id</span>=<span style="color:#98c379">"@+id/secondButton"</span>
<span style="color:#d19a66">android:layout_width</span>=<span style="color:#98c379">"match_parent"</span>
<span style="color:#d19a66">android:layout_height</span>=<span style="color:#98c379">"wrap_content"</span>
<span style="color:#d19a66">android:layout_marginBottom</span>=<span style="color:#98c379">"8dp"</span>
<span style="color:#d19a66">android:layout_marginTop</span>=<span style="color:#98c379">"8dp"</span>
<span style="color:#d19a66">android:layout_weight</span>=<span style="color:#98c379">"1"</span>
<span style="color:#d19a66">android:text</span>=<span style="color:#98c379">"退出当前页面"</span>
<span style="color:#d19a66">app:layout_constraintBottom_toBottomOf</span>=<span style="color:#98c379">"parent"</span>
<span style="color:#d19a66">app:layout_constraintTop_toBottomOf</span>=<span style="color:#98c379">"@+id/unityLayout"</span>
<span style="color:#d19a66">tools:layout_editor_absoluteX</span>=<span style="color:#98c379">"154dp"</span> />
</<span style="color:#e06c75">LinearLayout</span>>
</code></span></span>
运行结果:
后记
- 还有Unity和Android的交互问题,网上教程很多,这里就不赘述了:
Unity调用Android,通过AndroidJavaClass,AndroidJavaObject;
Android调用Unity,通过UnityPlayer.UnitySendMessage(),但是在后边的导入过程中忽然发现UnityPlayer访问不到了,后边去gradle工程找到了unityPlayerActivity 发现被封装为了mPlayerActivity