Version: 2021.1
Android
将 Unity 集成到 Android 应用程序中

Android 环境设置

要为 Android 构建和运行应用程序,必须安装 Unity Android Build Support 平台模块。还需要安装 Android 软件开发工具包(SDK)和原生开发工具包(NDK)才能在 Android 设备上构建和运行代码。默认情况下,Unity 会安装基于 OpenJDK 的 Java 开发工具包。

__注意__:Unity 支持 Android 4.4 “KitKat” 和更高版本。请参阅 AndroidSdkVersions 以了解详细信息。

Install Android Build Support and the Android SDK & NDK tools

Use the Unity Hub to install Android Build Support and the required dependencies: Android SDK & NDK tools, and OpenJDK.

You can install Android Build Support, the Android SDK & NDK tools, and OpenJDK when you install the Unity Editor, or add them at a later time.

For information on how to add the Android modules:

Enable USB debugging on your device

To enable USB debugging, you must enable Developer options on your device. To do this, see Android’s Configure developer options documentation.

Use a USB cable to connect your device to your computer. If you are developing on a Windows computer, you might need to install a device-specific USB driver. See the manufacturer website for your device for additional information.

The setup process differs for Windows and macOS. For more information on connecting your Android device to the SDK, refer to the Run Your App section of the Android Developer documentation.

为 Chrome OS 设备进行 ADB 调试

有关如何为 Chrome OS 设备设置开发环境并启用 ADB 调试的信息,请参阅 Google 文档 Chrome OS 开发者环境 (Chrome OS Developer Environments)

连接到您的 Chrome OS 设备

在部署到设备之前,您需要通过设备的 IP 地址手动连接 ADB。

首先需要 IP 地址。打开 Settings 应用并在边栏中选择 Network。然后单击激活的网络接口。

如果该接口为无线连接,接下来选择要连接到的 SSID。您将在详细信息中看到 IP 地址。请记下该地址。

如果使用的是有线连接,您的 IP 地址应显示在第一个详细信息页面上。

现在,您可以进行连接了。对于本示例,假定设备的 IP 为 192.168.0.100。在 shell 或 terminal 中运行以下命令

adb connect 192.168.0.100

If the connection was successful you will see a message such as connected to 192.168.0.65:5555. And adb devices should verify that the device is connected.

List of devices attached 192.168.0.65:5555 device

从现在起,您可以像通过 USB 连接一样运行 ADB 命令来定位设备。

注意: 如果您的设备或主机进入睡眠状态或失去网络连接,您可能需要重新连接。

自定义 Android SDK & NDK Tools 和 OpenJDK 的安装

You should use the Unity Hub to install Android SDK & NDK tools, to ensure that you receive the correct versions and configuration. Unity installs Android SDK & NDK Tools and OpenJDK respectively in the SDK, NDK and folders under /Unity/Hub/Editor/[EditorVersion]/Editor/Data/PlaybackEngines/AndroidPlayer/.

If you have multiple versions of Unity with the same required dependencies (check System requirements for the latest) and you want to avoid duplicating the installation of Android SDK & NDK Tools and OpenJDK, you can specify a shared location in the Unity Preferences window. To do this, go to Preferences > External tools and enter the directory paths in the SDK and NDK fields:

Preferences 窗口中显示了 Android 外部工具设置
Preferences 窗口中显示了 Android 外部工具设置

__警告__:Unity 未正式支持除了提供的 OpenJDK、SDK 或 NDK 版本外的其他版本。

要更改 Unity 用于构建 Android 应用程序的 OpenJDK、SDK Tools 或 NDK,请执行以下操作:

  1. Open your Unity project.

  2. Open the Preferences window (Windows and Linux: Edit > Preferences; macOS: Unity > Preferences).

  3. In the left navigation column, select External Tools.

  4. Change the OpenJDK path:
    1. Disable the JDK Installed with Unity (recommended) setting.
    2. In the JDK field, enter the path to the JDK installation folder, or use the Browse button to locate it.
  5. Change the Android SDK Tools path:
    1. Disable the Android SDK Tools Installed with Unity (recommended) setting.
    2. In the SDK field, enter the path to the SDK installation folder, or use the Browse button to locate it.

    Note: Unity works with the most recent version of the Android SDK available at the time of the Unity version release.

  6. Change the Android NDK path: 1.取消选中 Android NDK Installed with Unity (recommended)
    1. In the NDK field, enter the path to the NDK installation folder, or use the Browse button to locate it.

Each version of Unity requires a specific version of the Android NDK:

Unity 版本 NDK 版本
2018.4 LTS r16b
2019.4 LTS r19
2020.3 LTS r19
2021.1 r21d

请参阅系统要求页面以查看完整要求。

更新 Android SDK Target API

Unity Hub 安装了 Google Play 所需的最新版本的 Android SDK Target API。

如果您需要使用更新的版本,您可以从 Player Settings 窗口的 Target API Level字段更改 Target API(菜单:Edit > Project Settings > Player,然后选择 Android 平台)。您可以在 Other Settings > Identification 部分找到 Target API Level 选项。

为 Android SDK 选择目标 API
为 Android SDK 选择目标 API

选择默认选项以外的选项后,Unity 会提示您更新 Android SDK API。可选择执行以下操作之一:

  • 更新 Android SDK
  • 继续使用已安装的最高版本的 Android SDK

If you select an older version of the Target API, the Unity Android SDK Updater won’t be able to perform the update and gives an error message.

In this case, to update the Android SDK Target API, you must use the Android sdkmanager from either Android Studio or the command line tool. Regardless of the method you choose, make sure to select the correct Android SDK folder for Unity in the Edit > Preferences > External Tools window.

Important: On Windows, if you installed the Unity Editor in the default folder (/Program Files/), you must run the sdkmanager with elevated privilege (Run as Administrator) to perform the update.

Android
将 Unity 集成到 Android 应用程序中