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

应用程序性能分析

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

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

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

但是,每次要改进其性能元素时都构建应用程序可能会非常耗时。因此,要快速评估应用程序的性能,可以直接在编辑器中以运行模式对它进行性能分析。在运行模式下进行性能分析不会准确反映应用程序在真实设备上表现出的性能,但它是一个有用的工具,可在终端平台上进行初始分析后快速检查进行的更改是否可提高应用程序的性能。

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

Profile your application on a target platform

要在目标发布平台上分析您的应用程序,请将目标设备连接到网络或通过线缆直接连接到计算机。还可以通过 IP 地址连接到设备。只能以开发版形式对应用程序进行性能分析。要进行此设置,请打开 Build Settings(菜单:File > Build Settings),然后选择应用程序的目标平台。启用 Development Build 设置。启用此设置后,与性能分析器相关的以下两个设置将变为可用状态:Autoconnect to ProfilerDeep Profiling Support

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

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

如果还启用了 Deep Profiling Support 设置,Unity 会在构建的播放器启动时执行深度性能分析,这意味着性能分析器会对代码的每个部分进行性能分析,而不仅仅是在 ProfilerMarker 中显式包裹的代码时序。这对于获取有关应用程序启动时间的深度性能分析信息很有用,但是,这会为构建增加少量开销。

在 Profiler 窗口中连接到播放器

To use the Profiler to manually connect to a platform running your application, configure the settings in the Attach to Player dropdown. You can only do this if Autoconnect Profiler is disabled.

For a platform to appear in the Attach to Player dropdown, it must meet the following requirements:

  • The platform is visible on your local network or directly connected to your local machine by cable.
  • The platform is running a Player that you built with the Development Build option enabled. For instructions on how to enable this setting for a platform, refer to Profile your application on a target platform.
  • The platform is running your Player.

The Attach to Player dropdown displays all Unity Players that Unity detects through the network or by direct connection. You can identify these Players by Player Name and the Product Name that is running the Player; for example, iPhonePlayer (My iPhone).

You can also connect to a Player directly via its IP address. To do this, select the Attach to Player menu, and then select <Enter IP> in the dropdown. In the dialog box that appears, type the IP address and (optionally) the port of the Player you want to connect to.

To collect profiling information on your application, select the Player from the dropdown menu, and then click Record.

To continuously collect data while your application runs, enable the Run In Background setting in Player Settings (menu: Edit > Project Settings > Player > Resolution and Presentation). When you enable this setting, the Profiler collects data even when you leave your application running in the background. If you disable it, the Profiler only collects data when the application is running in an active window.

Using the Attach to Player search bar

The Attach to Player dropdown includes a search bar that you can use to find information about your Player. You can search by Player Name or device category, for example Remote. When you search by category, the result displays all devices in that category.

Select the name of a development Player to view it in the profiler.

Attach to Player properties

Each column provides the following information about when it is available:

属性: 描述:
Player Name The name of the device that is running your application.

To change this name, navigate to Edit > Preferences > Analysis > Profiler (macOS: Unity > Settings > Analysis > Profiler) and enter your desired name in the Custom Connection ID field.

You can also set a Player Name set when you launch a Player from the command line using the -connection-id argument.

For more information about the categories in this property, see Player name device categories
Product Name This is the value of the field that you set in Project > PlayerSettings.
IP The IP address of the Player.
Port The port of the Player.

Player name device categories

The Player Name category includes the following categories that display information about particular device types:

属性: 描述:
Play Mode Select this property to profile your application in Play Mode.
Edit Mode Select this property to profile the Unity Editor.
Local This list contains any device that is running on the local machine, in the Unity Editor or in a standalone player.

It also displays information for players that are physically connected to the host machine with a cable.
Remote This section displays information for devices running on the local network.

This section only appears when Unity finds a remote device running on the local network.
Connections without ID This section only appears when Unity finds a device running a player older than Unity 2021.2.
These players do not have Product Name, IP, or Port information.
Direct Connection Use this option to connect to a specific IP and port combination. This category displays the most recent IP you connected to.

平台特定性能分析指南

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

Web

You can use the Unity Profiler in the Web platform, but you cannot attach to a running player built with Web via the Editor. This is because the WebGL API 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 Web applications.

移动设备上的性能分析

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

When you set up remote profiling, the Unity Editor might sometimes not connect to the device automatically. If this happens, you can initiate the Profiler connection manually by selecting the Attach to Player dropdown menu in the Profiler window and choosing the appropriate device.

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

iOS remote profiling

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

  1. 将 iOS 设备连接到 WiFi 网络。性能分析器会使用本地 WiFi 网络将性能分析数据从设备发送到 Unity Editor。
  2. Attach your device to your computer via cable. In the Build Settings window (menu: File > Build Settings), select the Development Build and Autoconnect Profiler checkboxes, then click Build & Run.
  3. When the application launches on the device, open the Profiler window in the Unity Editor (menu: Window > Analysis > Profiler).

Android remote profiling

Android devices support two methods of remote profiling: WiFi and Android Debug Bridge (adb).

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

  1. 在 Android 设备上禁用移动数据。
  2. Connect your Android device to your WiFi network. The Profiler uses a local WiFi network to send profiling data from your device to the Unity Editor.
  3. Attach your device to your computer via cable. In the Build Settings window (menu: File > Build Settings), select the Development Build and Autoconnect Profiler checkboxes, then click Build & Run.
  4. When the application launches on the device, open the Profiler window in the Unity Editor (menu: Window > Analysis > Profiler).

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

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

  1. 确保设备处于开发模式并启用 USB debugging 设置
  2. 通过线缆将设备连接到计算机,并确保在 adb 设备列表中显示了该设备。
  3. In the Build Settings window (menu: File > Build Settings), select the Development Build checkbox, and then click Build & Run.
  4. 应用程序在设备上启动时,请在 Unity 编辑器中打开 Profiler 窗口(菜单:Window > Analysis > Profiler)。
  5. From the Attach to Player dropdown menu, select AndroidProfiler(ADB@127.0.0.1:34999). The entry in the dropdown menu is only visible when the selected target is 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'

ChromeOS remote profiling

ChromeOS devices support remote profiling through adb over WiFi, unlike Android devices that support remote profiling through both WiFi and adb over USB connection.

To enable WiFi profiling on ChromeOS devices, follow these steps:

  1. Enable Linux Development Environment and ADB debugging settings on your Chromebook. You need not enable ChromeOS Developer Mode. For more information on how to do this, refer to Enable ADB Debug.
  2. Connect your Chromebook and the host computer running Unity Editor to the same WiFi network.
  3. Establish a connection between your Chromebook and the host computer through adb. For more information on how to do this, refer to Connect to ADB over a network.
  4. In the Build Settings window (menu: File > Build Settings), select the Development Build and Autoconnect Profiler checkboxes, and then click Build & Run.
  5. When the application launches on the device, open the Profiler window in the Unity Editor (menu: Window > Analysis > Profiler).
    Note: If your application is not deploying to the Chromebook, check the Run Device dropdown in the Build Settings. If your device isn’t listed, but the adb connection between the host computer and Chromebook is established, enter the IP address of the device through the <Enter IP> option in the Run Device dropdown. After you enter the IP address, you can profile your application as usual.

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

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

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

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

每当在编辑器中进行性能分析时,都应确保在最大化视图中打开运行模式,并减少打开的编辑器窗口的数量。这可确保其他编辑器窗口不会在渲染线程和 GPU 上耗尽时间,从而影响性能数据。当运行模式处于最大化视图时,它会以更接近目标设备的分辨率运行应用程序,这会直接影响性能问题,例如与填充率相关的问题。

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

性能分析器的默认目标是运行模式,这会在编辑器运行运行模式时记录活动。运行模式性能分析可用于测试快速更改而无需重新构建播放器,但是不应用于替代验证应用程序目标平台和设备上的构建。这是因为运行模式在与编辑器相同的应用程序和主线程中运行,这意味着在运行模式下进行性能分析时,编辑器的系统(例如 UI、Inspector、Scene 视图渲染和资源管理)会影响应用程序的性能和内存性能分析测量。

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

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

PlayerLoop 和 EditorLoop 样本

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

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

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

重要信息:如果使用深度性能分析并以运行模式为目标,则它对在 PlayerLoop 和 EditorLoop 中发生的每个函数调用都会产生性能影响。这是因为深度性能分析会在域重新加载时挂接到任何脚本方法调用的开始和结束,不会检测哪些部分从未从 PlayerLoop 进行调用。EditorLoop 中发生的方法调用不会产生完整的样本创建开销,但它们仍会检查是否应发出样本,这会导致较小但仍然存在的开销。

对编辑器进行性能分析

将性能分析器的目标更改为编辑器时,之前隐藏在 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 do not affect your profiling data:

  • Only add the Profiler modules related to the area that you want to investigate to the Profiler window. To add and remove modules to the Profiler, select the dropdown in the top left of the Profiler window.
  • Avoid using Deep Profiling, because it might create a high overhead when you use it. If you want to see more details on samples with markers such as GC.Alloc or JobFence.Complete, go to the Profiler window toolbar and enable the Call Stacks setting. This provides the sample’s full call stack, which gives you the information you need without incurring the overhead of Deep Profiling.
  • Disable the Live setting, if you do not need to see the Hierarchy or Timeline view updating as the Profiler collects data. To see the data update in the window, you can stop recording.
  • 使用 F9 快捷键启用或禁用性能分析器。可以使用此快捷键捕获性能数据,而无需打开 Profiler 窗口。如果打开了独立性能分析器,则使用此快捷键会在此窗口中开始记录。

  • 2019.3 中添加了 Deep Profiling Support NewIn20193
性能分析器概述
常见性能分析器标记