继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

将unity5.6项目导入到android studio项目中

忽然笑
关注TA
已关注
手记 191
粉丝 64
获赞 275

1.首先在unity中打开 File - Build Settings

注意划红线部分的设置

export之前注意先要点开player settings 设置 company name 和 product name, 下面的package name也对应更改


2.export 之后的目录如下



  • assets整个拷贝到Android项目的/app/src/main/目录下。

  • libs中的unity-classes.jar以及其他jar文件拷贝到/app/lib/目录下。并Add As Library

  • libs中的armeabi-v7a以及x86(如果有的话)连同文件夹一起拷贝到/app/src/main/jniLibs/目录下。


3. 新建一个 activity 用来显示unity界面

代码如下

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    style="@android:style/Theme.Black.NoTitleBar"
    >


    <LinearLayout
        android:id="@+id/ll_unity_container"
        android:layout_width="300dp"
        android:layout_height="400dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:orientation="vertical"
        android:background="#f0f0f0"
        />


    <Button
        android:id="@+id/btn_rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll_unity_container"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="一个button"
        android:textSize="17sp"
        />

</RelativeLayout>


activity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

public class unityactivity extends UnityPlayerActivity {

    private LinearLayout mLlUnityContainer;
    private Button mBtnRotate;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_unityactivity);
        initView();
    }

    private void initView() {
        mLlUnityContainer = (LinearLayout) findViewById(R.id.ll_unity_container);
        mBtnRotate = (Button) findViewById(R.id.btn_rotate);

        //将Unity的View添加到布局里
        View scene = mUnityPlayer.getView();
        mLlUnityContainer.addView(scene);

        mBtnRotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //向Unity的Cube对象上的脚本里的RotateCube方法发送消息
                //第三个参数是传递的消息
                //通过UnitySendMessage向Unity发送消息,
                //三个参数分别是接收消息的GameObject,MethodName,参数;
                //UnityPlayer.UnitySendMessage("Cube", "RotateCube", "80");
            }
        });
    }
}



最终显示效果:



这里讲一下踩到的几个坑

1.大多数教程提到了在build project之前勾选google android project

然而unity5.6并没有该选项,取而代之的的是build system下的ADT选项,该选项功能与之前的google android project相同



2.当我打开unityactivity的时候显示your hardware does not support this application弹窗,无法正常打开

这是so文件不同导致的

由于unity导出的项目中只有armeabi-v7a版本的so,所以我把安卓工程中原有的其他平台的so都删除了只剩下了

这样就可以正常打开该activity了

原文出处

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP