Android集成Unity

前期准备材料
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); }


最后

如果想要成为架构师或想突破20~30K薪资范畴,那就不要局限在编码,业务,要会选型、扩展,提升编程思维。此外,良好的职业规划也很重要,学习的习惯很重要,但是最重要的还是要能持之以恒,任何不能坚持落实的计划都是空谈。

如果你没有方向,这里给大家分享一套由阿里高级架构师编写的《Android八大模块进阶笔记》,帮大家将杂乱、零散、碎片化的知识进行体系化的整理,让大家系统而高效地掌握Android开发的各个知识点。
在这里插入图片描述
相对于我们平时看的碎片化内容,这份笔记的知识点更系统化,更容易理解和记忆,是严格按照知识体系编排的。

全套视频资料:

一、面试合集

在这里插入图片描述
二、源码解析合集
在这里插入图片描述

三、开源框架合集
在这里插入图片描述
欢迎大家一键三连支持,若需要文中资料,直接扫描文末CSDN官方认证微信卡片免费领取↓↓↓

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值