在Android studio 中创建新工程
创建新工程
- 设定的Mininum SDK不要太高,我设定的API 21
在工程中引入Unity的接口包
接口包的位置可能随版本有所不同我用的是 Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes
- 把目录下的classes.jar放入新建的工程的app\libs下
在项目管理器调成project模式比较好找位置
TODO:加个图
- 右键classes包Add as Library,把包加入环境中
修改MainActivity为UnityPlayerActivity
接下来我们要把MainActivity继承于UnityPlayerActivity,让他在Unity项目打开的时候就启动并在这个类中提供打开新的有界面的Activity的接口
- 改变MainActivity的继承并删除setContentView方法(因为我们一开始不需要界面,只需要它能启动,不删掉会直接出现Android界面就没有我们想要的效果了)
public class MainActivity extends UnityPlayerActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
创建新界面的Activity和layout并在MainActivity中写出相应的调用函数
- 新创建一个java类作为新的Activity并创建相应的layout
KouyuActivity.java
package me.acring.mainkouyu;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by Acring on 2017/3/17.
* 口语练习
*/
public class KouyuActivity extends Activity{
@Override
public void onCreate(Bundle saveIntentState){
super.onCreate(saveIntentState);
setContentView(R.layout.activity_kouyu);
}
}
activity_kouyu.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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="口语练习"/>
</LinearLayout>
- 在MainActivity中添加函数
public void startMyActivity(){
Intent intent = new Intent(MainActivity.this, KouyuActivity.class);
startActivity(intent);
}
配置AndroidManifest.xml
- 配置Activity
<activity
android:name=".KouyuActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
>
</activity>
- 配置UnityActivity
- 添加
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
- 注意添加位置
<activity android:name=".MainActivity">
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
修改style.xml的配置
- 打开style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
- 以上全部删去修改为
<style name="AppTheme" parent="android:Theme.Light">
</style>
目的是去掉对V7包的引用,否则Unity在Build的时候会报错
- 上述步骤会使layout显示以下警告,目前不知道怎么解决,但是对程序运行似乎没有影响
Missing styles. Is the correct theme chosen for this layout?
Use the Theme combo box above the layout to choose a different layout, or fix
the theme style references.
修改build.gradle文件
- 在build.gradle文件(Module:app)顶部类似线面的代码中:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "me.acring.mainkouyu"
- 将
apply plugin: 'com.android.application'
修改为apply plugin: 'com.android.library'
- 注释掉
applicationId "me.acring.mainkouyu"
- 删除:
compile 'com.android.support:appcompat-v7:25.0.0'
testCompile 'junit:junit:4.12'
做完之后程序不能单独在AS运行并且Junit程序会报错,不过都是正常的
Build APK并取出关键文件
- 最后一步 Sync Now 并 Build APK
- 可能提示
Error:(6, 30) 错误: 程序包android.support.v7.app不存在
- 把
import android.support.v7.app.AppCompatActivity;
注释掉就行了 - 取出
\app\build\outputs\aarapp-debug.aar
文件和\app\src\main\AndroidManifest.xml
文件(放到一个临时位置) - 用压缩软件打开aarapp-debug.aar文件并删除掉目录下的 \libs\classes.jar 文件。(避免重复引用)
安卓部分到此结束
Unity工程的配置和调用
创建目录并引用文件
- 新建Unity项目,在Assets目录下新建Plugins\Android\目录
- 把刚刚取出的两个文件放到该目录下
创建按钮并配置脚本
- 新建一个C#脚本,添加如下函数
public void startMyActivity()
{
//AndroidJavaClass:通过指定类名可以构造出一个类
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
// UnityPlayer这个类可以获取当前的Activity
// currentActivity字符串对应源码中UnityPlayer类下 的 Activity 变量名。
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
// 在对象上调用一个Java方法
jo.Call("startMyActivity");
}
- 把脚本配置到按钮上并绑定OnClick(具体方法应该都知道)
切换平台并配置必要参数
- 打开Build Settings切换到Android平台,打开Player Settings修改Bundle Indentifier为Android工程中AndroidManifest.xml下package的名称,修改Minimum API Level为Android工程对应的API级别
网友评论
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.muses.unityplugin/com.muses.unityplugin.KouyuActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:812)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752)
at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:414)
at android.app.Activity.setContentView(Activity.java:2497)
at com.muses.unityplugin.KouyuActivity.onCreate(KouyuActivity.java:12)
打包的arr文件中有相应的资源,请问是什么问题