Version: 2019.2
Getting started with the Profiler window
CPU Usage Profiler module

应用程序性能分析

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

Remote profiling

You can only profile your application as a Development Build. To set this up, go to Build Settings (File > Build Settings) and select your application’s target platform. Enable the Development Build setting.

You can also check the Autoconnect Profiler checkbox to make the Unity Editor bake its IP address into the built Player during the build process. When you start the player, it attempts to connect to the Profiler to the Editor located at the baked IP address.

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

When you build and run your application, the player appears in the Attach to Player drop-down of the Profiler window. The Attach to Player drop-down shows all Unity Players running on your local network. You can identify these Players by player type and the hostname that is running the player (for example, 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

You can use the Unity Profiler in WebGL, but you cannot attach to running players via WebGL. This is because WebGL uses WebSockets for communication, which does not allow incoming connections on the browser side. To attach to a running player, you need to enable the Autoconnect Profiler checkbox in Build Settings (menu: File > Build Settings). Unity cannot profile draw calls for 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'

Getting started with the Profiler window
CPU Usage Profiler module