Unity3d 与 android 集成 [英] Unity3d integration with android

查看:64
本文介绍了Unity3d 与 android 集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个 android 应用程序,它由 Unity3d(动画等)和 AndroidSDK 上编写的部分组成(通过 androidSDK,我的意思是很少有用 java、清单和资源编写的活动)

I need to create an android application which consists of parts written on Unity3d (animation and so on) and on AndroidSDK (by androidSDK I mean few activities written in java, manifest and resources)

事实上我已经有了这些部分(至少模拟已经准备好了=))我不知道如何在它们之间进行通信.

In fact I already have those parts (At least mocks are ready=)) And I can't figure out how to communicate between them.

我已经阅读了很多关于 unity3d android 集成的文章(主要是关于插件"的东西)并且无法理解这个工作人员是如何工作的=(我理解的主要内容(除了官方参考之外的每个人都写过)是关于插件的文档很差... =)

I've read a lot of articles about unity3d android integration (mostly about things called "plugins") and failed to understand how this staff works=( The main thing I understood (everyone exept official refs wrote about that) is that documentation about plugins is very poor... =)

我能理解的唯一通信方式是从 unity3d 脚本(使用 AndroidJavaClass 和 AndroidJavaObject)制作一个意图(广播),以便我的活动(用 java 编写)可以处理它.但有些事情告诉我这不是最好的解决方案......

The only way to communicate which I can understand is to make an intent (broadcast) from unity3d script (using AndroidJavaClass and AndroidJavaObject) so my activity (written in java) can handle it. But something tells me it's not the best solution...

我读到的关于这个问题的内容是我需要使用 JNI(这是 java 本机接口 - 为什么?!? - 为什么我需要使用本机代码?)

What I read about the problem is that I need to use JNI (which is java native interface - WHY?!? - why do I need to use native code?)

我是否需要在 java 上创建一些中间级代码,目的是与我的 java 活动通信,使用 AndroidNDK 编译它并作为插件包含到我的 Unity3d 项目中?我怎么能写那个中间层呢?我是否需要使用 UnityPlayer 而不是Activity"作为基类,为什么?

Do I need to create some middle-level code on java which purpose is to communicate with my java activity, compile it using AndroidNDK and include into my Unity3d project as a plugin? How can I write that middle layer than? Do I need to use UnityPlayer instead of "Activity" as a base class and why?

推荐答案

这里有一个在普通 Android 应用中运行 Unity.

运行 Unity 有一个很棒的教程在 Android 视图中.一旦你启动并运行它,你就可以开始在你的应用程序的任何地方轻松地嵌入场景.

There is a great tutorial on running Unity inside of Android Views. Once you get this up and running you can start embedding scenes easily anywhere in your App.

当您需要从 Unity 调用 Java Android 应用时,您可以添加以下代码:

When you need to call into the Java Android app from Unity, you can add this code:

AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

然后您可以通过活动 AndroidJavaObject 在活动上调用任何您想要的方法.像这样的:

And then you can call any method you want on your activity through the activity AndroidJavaObject. Something like this:

activity.Call("yourFunctionName", parameters);

这篇关于Unity3d 与 android 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆