Unity3d 与 Android 的集成

发布于 2024-12-10 04:43:34 字数 586 浏览 0 评论 0原文

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

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

我读过很多关于unity3d android集成的文章(主要是关于所谓的“插件”的东西),但未能理解这个工作人员是如何工作的=(我理解的主要内容(除了官方参考文献之外的每个人都写过)是关于插件的文档非常差...=)

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

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

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

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.

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... =)

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

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?)

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

御弟哥哥 2024-12-17 04:43:34

这是有关 在普通 Android 应用程序中运行 Unity

有一个关于 在 Android 视图内运行 Unity。一旦您启动并运行它,您就可以开始轻松地将场景嵌入到您的应用程序中的任何位置。

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

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

然后您可以通过 Activity AndroidJavaObject 在您的 Activity 上调用您想要的任何方法。像这样的事情:

activity.Call("yourFunctionName", parameters);

Here is a tutorial on the basics of running Unity inside of a normal Android app.

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.

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");

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

activity.Call("yourFunctionName", parameters);
江城子 2024-12-17 04:43:34

为了完全支持Android,您不需要在java上创建一些中间层代码。

相反,您应该:

  1. 在 Android SDK 上创建一个 Lib 项目。
  2. 创建 1 个主要 Activity 扩展 UnityPlayerActivity
  3. 创建您需要的其他 Activity 并将其添加到 Manifest 中。
  4. 创建资源、布局...
  5. 导出到 JAR 并将其添加到 Unity,方法是将所有 JAR、资源文件复制到 Assets/Plugins/Android 文件夹。

此方法有助于编写推送通知、应用程序计费等......

此处参考#:http ://docs.unity3d.com/Documentation/Manual/PluginsForAndroid.html(->扩展UnityPlayerActivity Java代码)

此外,通过使用下面的代码:

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

您可以在 Unity 上使用 Android 完成大部分神奇的事情

注意*:yourFunctionName 必须在从 UnityPlayerActivity 扩展的主活动中实现(请参阅步骤 2)

问候,

For fully supporting on Android, you don't need to create some middle-level code on java.

Instead, you should:

  1. Create a Lib Project on Android SDK.
  2. Create 1 main Activity Extends UnityPlayerActivity
  3. Create others Activities that you need and add it into Manifest.
  4. Create resource, layout…
  5. Export to JAR and add it into Unity by copy all JAR, resource file to Assets/Plugins/Android folder.

This method is helpful for writing Push notification, in app billing, etc…

Ref# here: http://docs.unity3d.com/Documentation/Manual/PluginsForAndroid.html (-> Extending the UnityPlayerActivity Java Code)

And in addition, by using below code:

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

You can do most of magic things with Android on Unity

Note*: yourFunctionName must be implemented inside your main activity which extend from UnityPlayerActivity (Look at step 2)

Regards,

入怼 2024-12-17 04:43:34

2020 年的 unity3D 现在根本不需要 android SDK。
你只需点击
文件->建筑设置->切换平台->建造。

然后你就可以创建你的Android应用程序了。

the unity3D in 2020 isn't needed android SDK at all nowadays.
you can just click
file -> building setting -> switch platform -> build.

and you can create your android app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文