本页说明如何使用“Unity 用作库”功能将 Unity 运行时库集成到 Android 应用程序中。
使用此功能可以在应用程序中包含 Unity 支持的功能(例如 3D/2D 实时渲染、AR 体验、3D 模型交互或 2D 迷你游戏)。Unity 运行时库公开了多种控件来管理何时以及如何在应用程序中加载、激活和卸载内容。
从 Unity 构建 Gradle 项目时,无需进行任何其他操作。
Unity 生成的每个 Android Gradle 项目都具有以下结构:
要将 Unity 集成到另一个 Android Gradle 项目中,必须通过 settings.gradle 文件将生成的 Android Gradle 项目的 unityLibrary 模块包含在您的 Android Unity 项目中。
此代码仓库包含示例项目和插件(演示如何将 Unity 集成到 Android 应用程序中)以及更多文档。
要控制播放器,请转发 Intent 以启动 Unity 活动并在必要时对其进行扩展。如需了解更多信息,请参阅有关 Intent 和 Intent 过滤器 (Intents and Intent Filters) 的 Android 开发者文档。还可以使用 UnityPlayer Java API。
IUnityPlayerLifecycleEvents 提供一种与 Unity Player 的两个重要生命周期事件进行交互的方式:
Application.Unload
或 UnityPlayer.unload()
卸载 Unity Player 时,应用程序调用 IUnityPlayerLifecycleEvents.onUnityPlayerUnloaded
。这会使 Unity Player 处于暂停状态,在该状态下它将卸载所有场景,但将其他所有内容保持在内存中。IUnityPlayerLifecycleEvents.onUnityPlayerQuitted
。此调用之后,运行 Unity 的进程将结束。可将 IUnityPlayerLifecycleEvents
的实例传递到 UnityPlayer 构造函数或者覆盖 UnityPlayer
和 UnityPlayerActivity
子类中的方法。
Unity 不会控制运行时生命周期,因此“Unity 用作库”可能并不适用于所有可能的用例。已知限制包括:
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.