# 将性能分析信息添加到代码中简介

> Use profiler markers and counters to collect specific data about your application.

默认情况下，[性能分析器窗口](/engine/6000.0/manual/analysis/profiler/window-reference.md)显示有关 Unity 原生代码的信息。它使用[内置性能分析器标记](/engine/6000.0/manual/analysis/profiler/markers.md)来组织和划分收集的性能数据。

您可以将性能分析器标记添加到自己的代码中，从而使用 [`ProfilerMarker` API](/engine/6000.0/script-reference/unity-profiling/profiler-marker.md) 在性能分析器窗口中显示在这些区域花费的时间。

您还可以将性能分析器计数器添加到代码中以收集应用程序中指标的数据，并使用[自定义性能分析器模块](/engine/6000.0/manual/analysis/profiler/collect-data/visualizing-data/customizing/creating-custom-modules.md)显示这些数据。如果需要跟踪应用程序中的性能变化，此功能十分实用。将性能分析器计数器添加到代码能够加快对性能问题的排查，因为您可以将计数器中的信息与 Unity 的内置计数器和检测数据结合起来使用。

要将包含元数据的计数器和标记添加到代码中，需要使用 [Unity Profiling Core](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest?subfolder=/manual/index.html) 包。

\*\*重要提示：\*\*因为 Unity Profiling Core 是核心包，所以在 Package Manager UI 中无法找到。如要安装包，请[按名称添加](/engine/6000.0/manual/packages-list/packages/upm-ui/actions/quick.md)，即 `com.unity.profiling.core`。您也可以[通过此链接](/engine/6000.0/manual/com-unity-profiling.md)安装。

## 性能分析器标记

您可以使用 [`ProfilerMarker` API](/engine/6000.0/script-reference/unity-profiling/profiler-marker.md) 来标记资源消耗大的脚本代码块，使其在 Unity 性能分析器中可供查看，也可以使用 [Unity Profiling Core](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest?subfolder=/manual/index.html) 包中的 [`ProfilerMarker<TP1>` API](https://docs.unity3d.com/Packages/com.unity.profiling.core@1.0/api/Unity.Profiling.ProfilerMarker-1.html) 为生成的每个样本[添加额外元数据](/engine/6000.0/manual/um-profiler-add-markers-code.md#Adding-integer-or-floating-point-parameters-to-samples)。

这么做很有用，因为内置的 Unity 性能分析器不会对所有方法调用进行性能分析。另一种方法是[使用深度性能分析](/engine/6000.0/manual/analysis/profiler/collect-data/deep-profiling.md)，但这会导致大量开销，从而显著减慢应用程序执行速度并使结果产生偏差。使用 `ProfilerMarker` 是更加有效的标记代码的效方法。

`ProfilerMarker` 表示指定的性能分析器句柄，是最有效的分析代码的方法。您可以在应用程序的任何 C# 代码中进行使用。

Unity 在非开发构建中部署性能分析器标记时没有开销，因此您可以根据需要标记任意数量的样本。

Unity 使用 [`ConditionalAttribute`](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.conditionalattribute?view=net-8.0) 来标记 `Begin` 和 `End` 方法并对它们进行条件编译，因此在非开发构建中它们的执行开销为零。虽然 `Auto` 方法不会在非开发构建中完全被编译排除，但它们经过条件编译后仅返回 null，因此开销可以忽略不计。性能分析器标记的字段存在于发布构建中，占用的内存与其 [`IntPtr`](https://learn.microsoft.com/en-us/dotnet/api/system.intptr?view=net-8.0)（8 字节）相等。

[`ProfilerRecorder`](/engine/6000.0/script-reference/unity-profiling/profiler-recorder.md) API 还使用性能分析器标记来捕获性能数据。在 Unity 编辑器和开发播放器中进行开发期间，您可以使用 API 来了解代码的不同部分的性能概况，并找出性能问题。

## 性能分析器计数器

要在 Unity 性能分析器中显示自定义指标，请使用 [Unity Profiling Core](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest?subfolder=/manual/index.html) 包中的 `ProfilerCounter` API。性能分析器可以显示来自 [`ProfilerCounter`](https://docs.unity3d.com/Packages/com.unity.profiling.core@1.0/api/Unity.Profiling.ProfilerCounter-1.html) 或 [`ProfilerCounterValue`](https://docs.unity3d.com/Packages/com.unity.profiling.core@1.0/api/Unity.Profiling.ProfilerCounterValue-1.html) 的数据。

Unity 根据计数器配置文件的工作类型将性能分析器计数器分组，例如渲染、脚本或动画。您可以将自定义性能分析器计数器分配到 Unity 的任何性能分析类别。如需获取可用的性能分析器类别的完整列表，请参阅 [`ProfilerCategory`](/engine/6000.0/script-reference/unity-profiling/profiler-category.md)。

您可以使用 `ProfilerCounter` 或 `ProfilerCounterValue` API 来跟踪应用程序的指标，并使其在 Unity 性能分析器或其他代码中可见。如果您是资源商店的资源包开发者，则可以在代码中添加性能分析器计数器来帮助其他开发者了解系统的重要性能特征，而且他们可以使用这些信息对项目进行性能优化，或者合理规划资源使用。

下图展示了性能分析器计数器数据流的高级概述：


**性能分析器计数器流程。:**
![](/api/media?file=/engine/6000.0/media/images/profiler-counter-diagram.png)

[`ProfilerRecorder`](/engine/6000.0/script-reference/unity-profiling/profiler-recorder.md) API 在应用程序代码中检索性能分析器计数器数据，[`RawFrameDataView`](/engine/6000.0/script-reference/unity-editor-profiling/raw-frame-data-view.md) 或 [`HierarchyFrameDataView`](/engine/6000.0/script-reference/unity-editor-profiling/hierarchy-frame-data-view.md) API 在编辑器代码中获取性能分析器计数器数据。通过在模块编辑器中[配置自定义性能分析器模式](/engine/6000.0/manual/analysis/profiler/adding-information-code/creating-custom-counters.md)，可以在性能分析器窗口中显示此计数器数据。

`ProfilerCounter` 和 `ProfilerCounterValue` 支持以下类型：

* int
* long
* float
* double

## 其他资源

* [向代码中添加性能分析器标记](/engine/6000.0/manual/analysis/profiler/adding-information-code/add-markers-code.md)
* [将性能分析器计数器添加到代码中](/engine/6000.0/manual/analysis/profiler/adding-information-code/add-counters-code.md)
* [可视化性能分析器计数器](/engine/6000.0/manual/analysis/profiler/adding-information-code/creating-custom-counters.md)
* [`ProfilerMarker` API](/engine/6000.0/script-reference/unity-profiling/profiler-marker.md)
* [Profiling Core 包](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest)
