Android Studio将unity作为library库进行调用。

前言

近期需要查看三维模型,以前使用OpenGL ES进行开发,加载并显示,这次需要使用unity来进行场景的调用,由于主战场还是在手机端开发,这只是一个小功能,首先直接排除unity打包成apk的想法,然后上网找了很多的安卓调用unity,包括:as打包成aar给unity,相互传jar包,感觉很是麻烦,并且都是以前的版本了,unity2018和unity2019差距很大,没有找到合适的解决方案。
近期在bilibili翻到了将unity打包成库给as的教学视频,受益匪浅,记录一下。

环境

as版本: 4.0.1
unity版本:2019.4.8f1

参考学习地址

官方案例github地址:https://github.com/Unity-Technologies/uaal-example
官方案例文档:https://forum.unity.com/threads/integration-unity-as-a-library-in-native-android-app-version-2.751712/

官网案例的方法在这里不在赘述,以下为学习参考的地方:
bilibili讲解视频:https://www.bilibili.com/video/BV1QA411Y7N8/?spm_id_from=333.788.videocard.0
参考博客1(讲了一半):https://blog.csdn.net/qq_45888298/article/details/104209044
参考博客2(在官方代码的基础上做了一些更改):https://blog.csdn.net/qq_40028144/article/details/108058407

创建自己的工程并调用自己的unity

创建自己的安卓工程没有什么需要注意的,就正常创建就可以了。
创建自己的unity工程,为了方便操作,在这里我将安卓工程和unity工程放在一起了。
文件目录如下:
在这里插入图片描述
第一步我们需要先在我们的unity中进行操作:
打开File-》Build Settings
选择安卓,如果是没转变成安卓平台的项目还得转换成安卓平台:
在这里插入图片描述
点击 add open scenes 加入自己制作的场景,选择export project
在这里插入图片描述
然后点击左下角的在这里插入图片描述
更改名字:在这里插入图片描述
一般other settings中的包名会自动更改,如果没有自己更改,则自己手动更改,选择最低sdk,最好是和as中的最低相同。
在这里插入图片描述
继续更改other settings:两者都选上,官网上有问题说是AR崩溃的,解决方案就是选择IL2PP,选上ARM64。
在这里插入图片描述
更改publishing settings,必须有这个,不然不成功(可能吧),原来有的就引入,没有的就创建。
在这里插入图片描述
到这里设置完成,点击刚开始的export来导出:
在这里插入图片描述
在这里插入图片描述
在刚才创建的unity工程下新建一个文件androidBuild,用来存储导出的unity。
导出成功之后的androidBuild文件夹之中应该有以下东西:
在这里插入图片描述
可以看到这里的文件结构跟unity2018中导出的文件结构已经有了很大的不同了。
第二步我们需要在我们的as中进行操作:
这里的操作跟官网的案例一样,毕竟我也是照着学来的。。这里用照片来显示了,想要复制粘贴的话请翻到前面的链接,下载官方案例查看或者博客里面找。
修改settings.gradle
在这里插入图片描述
注意蓝色框中,我们需要写上自己刚才导出的unityLibrary路径。
修改bulid.gradle(:app)
在这里插入图片描述
修改bulid.gradle(UnityLibraryTest)
UnityLibraryTest是我的as工程名字。
在这里插入图片描述
然后点击左上角的在这里插入图片描述
同步完成之后,如果成功了,你将会拥有一个
在这里插入图片描述
到这里我们的unity的库就导入成功了,那么怎么用呢?
我们需要定义一个类:
在这里插入图片描述
这个类中可以添加一些对unity界面中的一些操作:
比如:
添加一个按钮并加入操作
在这里插入图片描述
在unity界面中通过手机返回键返回时需要重写:
在这里插入图片描述
调用在MainActivity中写:
在这里插入图片描述
在布局文件中进行函数调用:
在这里插入图片描述
最后一项更改Strings.xml
在这里插入图片描述
加入红色框的这一项,unity在自己的包中有这一项是固定的,需要在as中同样定义这一个字符串才能够成功的调用。(可能吧)
到这里不出什么意外的话应该大功告成,可以连上手机进行调用试试了

那么,这样调用,我们如果想要调整unity代码的话应该怎么办呢?总不能每次改一次代码重新引入一次吧。

我的方法是在原来引入的unity工程的基础之上,正常更改这个unity工程的代码,然后像上述的那样导出操作一样导出到原来的文件夹进行覆盖。
在这里插入图片描述
其余保持不变,然后as重新编译连接,unity的代码就改变了,as端不受影响,可以继续开发as端。

官方案例中的一些问题整理(机器翻译)

Q: @btristan:Looks like the way the unloading/quitting issues were addressed is by having Unity run in a separate process and explicitly killing it.

This was the workaround that my team had investigated but ultimately our client is unwilling to operate with the JVM split into multiple processes. This is because they use Android UI elements over the Unity view that need to access the objects from the app’s memory. To continue doing this, they would need to replicate their main process memory in the Unity process.

Ultimately we found other workarounds to use Unity in the same process but workarounds are not what we like to ship in production code. Will there be any solution (like tightening up Unity’s shut-down sequence) for apps that cannot be split?

看起来卸载/退出问题的解决方式是让Unity在单独的进程中运行并显式地终止它。

这是我的团队调查过的解决方案,但最终我们的客户不愿意将JVM拆分为多个进程。这是因为他们在Unity视图上使用Android UI元素,这些元素需要从应用程序的内存中访问对象。为了继续这样做,他们需要在Unity进程中复制他们的主进程内存。

最终我们找到了在同一个过程中使用Unity的其他解决方案,但这些方案并不是我们希望在生产代码中提供的。对于不能拆分的应用程序,会有什么解决方案(比如收紧Unity的关闭程序)?

A: IUnityPlayerLifecycleEvents introduced since 2019.3.b4+ provides easy way to react to two important lifecycle events of Unity Player:

• Unload - IUnityPlayerLifecycleEvents.onUnityPlayerUnloaded will be called when unity player will be unloaded though Application.Unload or UnityPlayer.unload(). (UnityPlayer in unloaded/paused state atm.)
• Quit - IUnityPlayerLifecycleEvents.onUnityPlayerQuitted is called when Unity player did quit. Please note process where unity was running will be killed after this call.
You can pass instance of IUnityPlayerLifecycleEvents to UnityPlayer constructor or override methods in subclasses of UnityPlayer or UnityPlayerActivity

@btristan you can try keep activity in same process and relay on Unload

IUnityPlayerLifecycleEvents自从2019.3.b4+提供了对Unity Player的两个重要生命周期事件做出反应的简单方法:
Unload卸载-当通过Application unload 或者UnityPlayer.unload(). (UnityPlayer处于卸载/暂停状态atm)时IUnityPlayerLifecycleEvents.onUnityPlayerRunLoaded将会被调用
Quit退出-当Unity player退出时,IUnityPlayerLifecycleEvents.onUnityPlayerQuitd将会被调用。请注意,unity运行的进程将在这个调用后被终止。

您可以将IUnityPlayerLifecycleEvents的实例传递给UnityPlayer的构造函数或者重写UnityPlayer 或者UnityPlayerActivity的子类中的方法

@btristan您可以尝试将activity保持在同一进程中,并relay on Unload

将这个unity视图作为子视图以及传递更多的交互信息:

学习参考:https://www.cnblogs.com/zhxmdefj/p/13273560.html
UnitySendMessagehttps://stackoverflow.com/questions/45775916/call-unity-function-from-java/45776104#45776104
利用接口安卓和unity传参调用https://stackoverflow.com/questions/32944478/callback-listener-in-unity-how-to-call-script-file-method-from-unityplayeracti/41018028#41018028

结语

本人是新手,不知道安卓在调用unity这方面的开发有没有完整详细的教程,自学一脸懵逼,希望有耐心大神指点。

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值