创建一个新的AndroidStudio app项目。

1.添加库文件:
拷贝unity安装目录下的库文件:Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\classes.jar
到安卓项目目录下:app\libs\unitylibs.jar

2.拷贝unity安装目录下的所有文件夹:Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Libs
到安卓项目目录下:app\src\main\jniLibs

3.拷贝资源
将unity项目的编译好的apk包解压后,把assets文件夹拷贝到安卓项目app\src\main中

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

<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:


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博客园HemJohn

将unity3d项目嵌入到Android App中使用的更多相关文章

  1. 如何将Eclipse中的项目迁移到Android Studio 中

    如何将Eclipse中的项目迁移到Android Studio 中 如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的 ...

  2. [转]-如何将Eclipse中的项目迁移到Android Studio 中

    英文地址:http://developer.android.com/sdk/installing/migrate.html 翻译:Android Studio 中文组(大锤译) 如果你之前有用Ecli ...

  3. studio--如何将Eclipse中的项目迁移到Android Studio 中

    如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的是生成Build Gradle的文件.因为Android Studi ...

  4. Eclipse项目导入到Android Studio中

    背景 最近需要将Eclipse中的android项目导入到Android Studio中!倒腾一番,记录如下! 步骤1 打开Android Studio(下文称AS),选择Import project ...

  5. 如何将Eclipse中的项目迁移到Android Studio中

    如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的是生成Build Gradle的文件.因为Android Studi ...

  6. (转)eclipse项目导入到android studio中

    原文:http://www.cnblogs.com/lao-liang/p/5016541.html?utm_source=tuicool&utm_medium=referral Androi ...

  7. Android app中的so库和CPU架构

    一.android目前有几种cpu架构? 早期的Android系统几乎只支持ARMv5的CPU架构,目前支持七种CPU架构:ARMv5,ARMv7 (从2010年起),x86 (从2011年起),MI ...

  8. 如何将Eclipse中的开源项目使用到Android Studio中

    近几日,笔者用到了一些开源项目,比如著名的PTR项目.但是在使用的过程中,遇到了一些问题. 这个开源库是在Eclipse上面写的,我们现在开发用的是Android stuido. 两种软件的项目结构是 ...

  9. 在Android App中集成Google登录

    技术文章 来源:码农网 发布:2016-09-19 浏览:194 摘要:今天,几乎所有的web和移动app都自带谷歌和Facebook登录,这对app开发者和用户来说是一个非常有用的功能,因为几乎每个 ...

随机推荐

  1. MySQL下载与安装配置

    一.Windows 上安装 MySQL Windows 上安装 MySQL 相对简单,最新版本下载地址: 官网:https://dev.mysql.com/downloads/mysql/ 下载步骤: ...

  2. 出现提示ERROR 1289 The 'InnoDB' feature is disabled; you need MySQL built with 'InnoDB' to have IT working

    关闭mysql数据库 在mysql的安装目录中找到my.ini文件找到skip-innodb,在前面加上#号保存,重启mysql服务 OK.

  3. C# string.Empty

    在C#中,如果赋值一个字符串为空白字符串,我们一般会用“”的形式对字符串进行赋值操作,其实在C#的字符串类String类中,有个专门的常量string.Empty来代表空字符串,可直接在赋值的时候使用 ...

  4. spring tx:advice事务配置

    http://blog.csdn.net/bao19901210/article/details/17226439 http://blog.csdn.net/rong_wz/article/detai ...

  5. JS——三种嵌入页面的方式

    一 行间事件 二 页面script标签嵌入 三 外部引入 <!DOCTYPE html> <html lang="en"> <head> < ...

  6. css文本换行的问题

    今天敲代码的时候发现了一个一直都没太注意的小问题,当我在一个200px的div中写了一长串的‘f ‘时发现没有换行 但加上空格或标点符号后就能自动换行 原来浏览器把它当成了一串完整的单词,所以默认不换 ...

  7. Vsftp设置为PASV mode(被动模式传送)

    首先配置vsftpd.conf文件: #vi /etc/vsftpd/vsftpd.conf 在文件的末尾加上: pasv_enable=YES pasv_max_port=30010 pasv_mi ...

  8. Linux关闭/开启防火墙命令

    Linux还是比较常用的,于是我研究了一下Linux关闭防火墙命令,在这里拿出来和大家分享一下,希望你能学会Linux关闭防火墙命令 . 1) 永久性生效,重启后不会复原 开启: chkconfig ...

  9. Tensorflow版Faster RCNN源码解析(TFFRCNN) (2)推断(测试)过程不使用RPN时代码运行流程

    本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记第二篇   推断(测试)过程不使用RPN时代码运行流程 作者:Jiang Wu  原文见:https://hom ...

  10. 十六进制和ASCII之间的转换

    2.关于两个byte[]数组的合并: public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) { byte[] byte_3 = n ...