Version: 2019.3
Getting started with the Profiler window
音频性能分析器 (Audio Profiler)

应用程序性能分析

要在目标发布平台上分析您的应用程序,请将目标设备连接到网络或通过线缆直接连接到计算机。也可以直接在 Unity Editor 中对应用程序进行性能分析,从而在应用程序开发期间获得大致性能分析结果。

Build Settings

只能以开发版形式对您的应用程序进行性能分析。要进行此设置,请打开 Build Settings (File > Build Settings),然后选择应用程序的目标平台。启用 Development Build 设置。启用此设置后,与性能分析器相关的以下两个设置将变为可用状态:__Autoconnect to Profiler__ 和 Deep Profiling Support

Build Settings 窗口,已启用性能分析选项
Build Settings 窗口,已启用性能分析选项

启用 Autoconnect Profiler 设置后,Unity Editor 在构建过程中将自己的 IP 地址烘焙到已构建的播放器中。播放器启动时会尝试将性能分析器连接到位于所烘焙的 IP 地址的 Editor。

如果还启用了 Deep Profiling Support 设置,Unity 将在已构建的播放器启动时进行深度性能分析。此功能可用于对应用程序的启动时间进行深度性能分析。这种情况下会增加少量的构建开销。

在 Profiler 窗口中连接到播放器

构建并运行应用程序时,播放器会显示在 Profiler 窗口的 Attach to Player 下拉选单中。Attach to Player 下拉选单会显示本地网络上运行的所有 Unity 播放器。通过播放器类型和运行播放器的主机名可以识别这些播放器(例如 iPhonePlayer (Toms iPhone))。

Attach to Player 下拉选单,其中包含一个网络上的播放器
Attach to Player 下拉选单,其中包含一个网络上的播放器

选择播放器,然后单击 Record 即可开始收集应用程序的性能分析信息。如果在 Build Settings 中已启用 __Autoconnect to Profiler__,Unity 会在应用程序启动时自动开始收集数据。

要在应用程序运行时持续收集数据,请启用 Player Settings 中的 Run In Background 设置(菜单:__Edit > Project Settings > Player > Resolution and Presentation__)。启用此设置后,即使让应用程序在后台运行,性能分析器也会收集数据。如果禁用此设置,性能分析器仅在应用程序在活动窗口中运行时才收集数据。

在 Unity Editor 中进行性能分析

如果使用 Profiler 窗口在 Editor 中运行应用程序并进行性能分析,则结果只是该应用程序在目标平台上运行时的行为的近似值。因为运行模式与 Editor 的运行过程相同,所以无法将应用程序的 CPU、GPU 和内存使用量数据与 Unity 的相应数据完全隔离。这会进一步导致得出的性能分析数据出现误差。

为了获得更好的性能分析结果,应始终在目标设备上对应用程序进行性能分析。请只在 Editor 中进行性能分析以便快速迭代设备上已经确定的问题。

WebGL

可以在 WebGL 中使用 Unity Profiler,但不能通过 Editor 连接到运行 WebGL 的播放器。这是因为 WebGL 使用 WebSocket 进行通信,所以不允许浏览器端的传入连接。要连接到运行的播放器,必须在 Build Settings__(菜单:__File > Build Settings__)中启用 Autoconnect Profiler__ 复选框。Unity 无法对 WebGL 的绘制调用进行性能分析。

移动设备上的性能分析

iOS 和 Android 设备均支持通过网络进行远程性能分析。如果使用了防火墙,请在防火墙的出站规则中开放端口 54998 至 55511。这些是 Unity 用于远程性能分析的端口。

注意:有时,在设置远程性能分析后,Unity Editor 可能无法自动连接到设备。如果发生此情况,可以手动发起性能分析器连接。为此,请选择 Profiler 窗口中的 Attach to Player 下拉菜单,然后选择相应设备。

也可以将目标设备直接插入计算机以避免网络或连接问题。

iOS 远程性能分析

要对 iOS 设备启用远程性能分析,请遵循以下步骤:

1.将 iOS 设备连接到 WiFi 网络。性能分析器会使用本地 WiFi 网络将性能分析数据从设备发送到 Unity Editor。 1.通过线缆将设备连接到计算机。打开 Build Settings__(菜单:__File > Build Settings__),选中 Development Build__ 和 Autoconnect Profiler 复选框,然后选择 Build & Run。 1.应用程序在设备上启动时,请在 Unity Editor 中打开 Profiler 窗口(菜单:__Window > Analysis > Profiler__)。

Android 远程性能分析

Android 设备支持两种远程性能分析方法:通过 WiFi 或通过 Android Debug Bridge (adb)

要进行 WiFi 性能分析,请遵循以下步骤:

1.在 Android 设备上禁用移动数据。 1.将 Android 设备连接到 WiFi 网络。性能分析器会使用本地 WiFi 网络将性能分析数据从设备发送到 Unity Editor。 1.通过线缆将设备连接到计算机。打开 Build Settings__(菜单:__File > Build Settings__),选中 Development Build__ 和 Autoconnect Profiler 复选框,然后选择 Build & Run。 1.应用程序在设备上启动时,请在 Unity Editor 中打开 Profiler 窗口(菜单:__Window > Analysis > Profiler__)。

注意:Android 设备和主机(运行 Unity Editor)必须位于同一子网上才能正常进行设备检测。

要进行 Android Debug Bridge (adb) 性能分析,请遵循以下步骤:

1.通过线缆将设备连接到计算机,并确保在 adb 设备列表中显示了该设备。 1.打开 Build Settings__(菜单:__File > Build Settings__),选中 Development Build__ 复选框,然后选择 Build & Run。 1.应用程序在设备上启动时,请在 Unity Editor 中打开 Profiler 窗口(菜单:__Window > Analysis > Profiler__)。 1.从 Attach to Player 下拉菜单中,选择 AndroidProfiler(ADB@127.0.0.1:34999)。仅当所选目标为 Android 时,下拉菜单中才会显示该条目。

选择 Build & Run 时,Unity Editor 会自动为应用程序创建 adb 隧道。如果要对另一应用程序进行性能分析,或者重新启动 adb 服务器,则必须手动配置此隧道。为此,请打开终端窗口或 CMD 提示窗口,并输入:

adb forward tcp:34999 localabstract:Unity-{insert bundle identifier here}

要将深度性能分析用于 Android 构建,必须启用 Android Player Settings 中的 Mono Scripting Backend 设置(菜单:__Edit > Project Settings > Player > Android > Other Settings__),然后输入以下内容以便通过 adb 命令来启动游戏:

~$ adb shell am start -n {insert bundle identifier here}/com.unity3d.player.UnityPlayerActivity -e 'unity' '-deepprofiling'


  • 2019.3 中添加了 Deep Profiling Support NewIn20193
Getting started with the Profiler window
音频性能分析器 (Audio Profiler)