Version: 2020.3
性能分析器概述
常见性能分析器标记

应用程序性能分析

使用 Unity 性能分析器对应用程序进行性能分析时,可以通过三种主要方式记录数据:

  • 在目标平台上的播放器中对应用程序进行性能分析
  • 在 Unity 编辑器中以运行模式对应用程序进行性能分析
  • 对 Unity 编辑器进行性能分析

获得有关应用程序的准确时序的最佳方法是在打算发布它的终端平台上对它进行性能分析。这样可提供有关影响应用程序性能的因素的准确时序。

However, it can be time-consuming to build your application every time you would like to improve elements of its performance. To quickly assess the performance of your application you can profile it directly in Play mode in the Editor. Profiling in Play mode doesn’t give you an accurate reflection of what the performance of your application looks like on a real device, but it’s a useful tool to quickly check to see if the changes you make improve your application’s performance, after initially profiling on the end platform.

Unity 编辑器可能会影响应用程序的性能,因为它在运行模式下运行时使用与应用程序相同的资源,因此还可以单独对编辑器进行性能分析以确定它使用的资源。如果应用程序仅设计为在运行模式下工作(例如用于电影制作),这会特别有用。

在目标平台上对应用程序进行性能分析

To profile your application on its target release platform, connect the target device to your network or directly to your computer via cable. You can also connect to a device via IP address. You can only profile your application as a Development Build. To set this up, go to Build Settings (menu: File > Build Settings) and select your application’s target platform. Enable the Development Build setting. When you enable this setting, two settings related to the Profiler become available: Autoconnect Profiler and Deep Profiling Support.

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

When you enable the Autoconnect Profiler setting, the Unity Editor bakes its IP address into the built Player during the build process. When you start the Player, it attempts to connect to the Profiler in the Editor located at the baked IP address.

If you also enable the Deep Profiling Support setting, Unity performs Deep Profiling when the built Player starts, which means that the Profiler profiles every part of your code, and not just code timings explicitly wrapped in ProfilerMarkers. This is useful to get Deep Profiling information on your application’s start up times, however, this adds a small amount of overhead to your build.

在 Profiler 窗口中连接到播放器

If you don’t enable the Autoconnect Profiler setting in the Build Settings, you can manually connect to a platform running your application. While your application is running in a player, 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’s running the player (for example, “iPhonePlayer (Toms iPhone)”).

还可以通过 IP 地址远程连接到播放器。为此,请选择 Attach to Player 菜单,然后在下拉选单中选择 <Enter IP>。一个对话框会出现,可以在其中键入要连接的播放器的 IP 地址和(可选)端口。

Attach to Player 下拉选单
Attach to Player 下拉选单

要开始收集有关应用程序的性能分析信息,请从下拉菜单中选择播放器,然后单击 Record。要在应用程序运行时持续收集数据,请启用 Player Settings 中的 Run In Background 设置(菜单:Edit > Project Settings > Player > Resolution and Presentation)。启用此设置后,即使让应用程序在后台运行,性能分析器也会收集数据。如果禁用此设置,性能分析器仅在应用程序在活动窗口中运行时才收集数据。

平台特定性能分析指南

将不同平台连接到 Unity 性能分析器时,它们会具有不同的行为方式。以下部分提供有关每个平台的一些常见行为的指导:

WebGL

You can use the Unity Profiler in WebGL, but you can’t attach to a running player built with WebGL via the Editor. This is because WebGL uses WebSockets for communication, which doesn’t 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 can’t profile draw calls for WebGL.

移动设备上的性能分析

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

Sometimes, when you set up remote profiling, the Unity Editor might not autoconnect to the device. If this happens, you can initiate the Profiler connection manually. To do this, select the Attach to Player drop-down menu in the Profiler window and choose the appropriate device.

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

iOS 远程性能分析

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

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

Android 远程性能分析

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

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

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

Note: The Android device and host computer that’s running the Unity Editor must both be on the same subnet for device detection to work.

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

  1. 确保设备处于开发模式并启用 USB debugging 设置
  2. 通过线缆将设备连接到计算机,并确保在 adb 设备列表中显示了该设备。
  3. 打开 Build Settings(菜单:File > Build Settings),选中 Development Build 复选框,然后选择 Build & Run
  4. 应用程序在设备上启动时,请在 Unity 编辑器中打开 Profiler 窗口(菜单:Window > Analysis > Profiler)。
  5. Attach to Player 下拉菜单中,选择 AndroidProfiler(ADB@127.0.0.1:34999)。仅当所选目标为 Android 时,下拉菜单中才会显示该条目。

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

  • 通过 USB 线缆建立编辑器到 Android 的连接时需要

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

  • 通过 USB 线缆建立 Android 到编辑器的连接时需要

    adb reverse tcp:34998 tcp:34999

要将深度性能分析用于 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'

Android Chrome OS 远程性能分析

Unity can’t automatically discover Chrome OS devices. To initiate a connection, connect to the device through Android Debug Bridge (adb) by its IP address and then select <Enter IP> from the Attach to Player drop-down menu and enter the device’s IP address. Once you have connected, you can profile your application as usual.

在 Unity 编辑器中进行性能分析

When you use the Profiler window to run and profile your application in the Editor, the results are only an approximation of your application’s behavior when the target platform runs it. This is because Play mode runs in the same process as the Editor, so you can’t fully isolate your application’s CPU, GPU, and memory usage from the Unity Editor’s usage. This skews the resulting profiling data.

要获得更好的性能分析结果,应该始终在目标设备上对应用程序进行性能分析,并且仅在编辑器中进行性能分析以快速迭代在设备上已确定的问题。

还可以在运行模式下进行性能分析或是对编辑器进行性能分析以确定与应用程序性能无关的问题,例如较长的加载时间或无响应的编辑器是否会减慢迭代时间,或者应用程序是否在运行模式下性能不佳。

Whenever you profile in the Editor, you should make sure that you open Play mode in maximized view, and reduce the amount of open Editor windows. This ensures that other Editor windows don’t use up time on the render thread and GPU, and therefore affect the performance data. When Play mode is in a maximized view it runs your application at a resolution closer to that of your target device, which directly affects performance issues such as those related to fill rate.

在运行模式下进行性能分析

The Profiler’s default target is Play mode, which records activities when the Editor is running Play mode. Play mode profiling is useful to test out quick changes without having to rebuild a player, but you shouldn’t use it as a replacement for validating builds on your application’s target platform and devices. This is because Play mode runs in the same application and main thread as the Editor, which means that when you profile in Play mode, the Editor’s systems such as the UI, Inspectors, Scene View rendering, and asset management affect the performance and memory profiling measurements of your application.

要在运行模式下有效地进行性能分析,应该定期构建应用程序并将它们部署到一系列目标设备(高规格和低规格设备),并在这些设备上对应用程序进行测试和性能分析。如果在这些设备上发现应用程序方面的性能问题,请缩小最需要关注的区域。

随后可以在运行模式下对应用程序进行性能分析,并快速迭代对应用程序进行的任何更改。可以使用通过在目标设备上对应用程序进行性能分析而获得的信息来确定在运行模式下对应用程序进行性能分析后是否存在任何类似行为。随后可以更改应用程序,并再次在运行模式下进行性能分析以快速查看更改所产生的效果。一旦对更改感到满意,请再次构建应用程序并部署到目标设备以验证更改。

PlayerLoop 和 EditorLoop 样本

为了减少编辑器在运行运行模式时创建的性能分析数据中的噪声和误导性测量数量,CPU 和 GPU 性能分析器模块将其时序拆分为在 PlayerLoopEditorLoop 中发生的时序。Unity 会向这些类型的性能分析器样本分配 PlayerLoop 和 EditorLoop 标记

当性能分析器以运行模式为目标时,它只收集发生在 PlayerLoop 内部发生的时序样本。

CPU 性能分析器模块图表中,Unity 将所有 EditorLoop 样本都分类为 Others。因此,EditorLoop 样本是该类别的最大贡献者。如果想了解编辑器在这段时间内的操作,并且还想获得有关 Others 类别的其他贡献者的更详细细分,请将性能分析器目标更改为编辑器。

Important: If you use Deep Profiling, and target Play mode, it has a performance impact on every function call that happens in both PlayerLoop and EditorLoop. This is because Deep Profiling hooks into the beginning and end of any scripting method call on domain reload and it doesn’t detect which parts are never called from the PlayerLoop. The method calls that happen in the EditorLoop don’t incur the full overhead of creating a sample, but they still check if they should emit one, which causes a smaller but still present overhead.

对编辑器进行性能分析

将性能分析器的目标更改为编辑器时,之前隐藏在 EditorLoop 标记下的所有样本都会归类到其各自的类别中。这意味着 CPU 性能分析器模块详细信息面板及其图表中的信息会显著更改。

要对编辑器的启动时间进行性能分析,请使用命令行选项 -profiler-enable 启动编辑器。

要减少 Profiler 窗口对编辑器性能的影响,可以使用独立性能分析器(它会在自己的进程中打开 Profiler 窗口)。如果选择编辑器作为性能分析目标,或是对应用程序进行深度性能分析,那么这会尤其有用,因为 Profiler 窗口本身通常使用可能会使性能数据出现偏差的资源。

对应用程序进行性能分析的最佳实践

When you profile your application, there are a few things that you can do to ensure consistency across profiling sessions, and to make sure that the processes that Unity uses doesn’t affect your profiling data:

  • 只添加与要调查到 Profiler 窗口的区域相关的性能分析器模块。要向性能分析器添加和移除模块,请选择 Profiler 窗口左上角的下拉选单。
  • 避免使用深度性能分析,因为它会在使用时产生较高开销。如果要查看有关带有标记(如 GC.Alloc 或 JobFence.Complete)的样本的更多详细信息,请导航到 Profiler 窗口工具栏并启用 Call Stacks 设置。这可提供样本的完整调用栈,其中会提供所需信息,而不会产生深度性能分析的开销。
  • Disable the Live setting if you don’t need to see the Hierarchy or Timeline view updating as the Profiler collects data. Stop recording to see the data update to the window.
  • 使用 F9 快捷键启用或禁用性能分析器。可以使用此快捷键捕获性能数据,而无需打开 Profiler 窗口。如果打开了独立性能分析器,则使用此快捷键会在此窗口中开始记录。

  • 2019.3 中添加了 Deep Profiling Support NewIn20193
性能分析器概述
常见性能分析器标记
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961