---
title: 将性能分析信息添加到代码简介中
product: Unity Editor
version: '6000.0'
latest_version: '7000.0'
status: This page documents version 6000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/zh-cn/engine/6000.0/manual/analysis/profiler/customizing/adding-information-code/intro
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/analysis/profiler/customizing/adding-information-code/intro.md
last_updated: 2026-10-30
generated_at: 2026-11-20T20:44:16.629Z
locale: zh-cn
---

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

> 使用性性能分析器Markers和计数器来收集有关应用程序程序的指定的数据。

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

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

还可以在代码中添加 Profiler 计数器以收集应用程序中 Metrics 的数据，并使用[自定义性能分析器模块](/engine/6000.0/manual/analysis/profiler/customizing/creating-custom-modules.md)显示这些数据。如果要轨道应用程序中的性能变化，这非常有用。将性能分析器计数器添加到代码可以加快性能问题的调查，因为您可以将计数器中的信息与 Unity 的内置计数器和检测数据结合使用。

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

> **Important:**
>
> 在 Package Manager UI 中找不到 Unity Profiling Core 包，因为它是包。要安装该包，请[按 Name 添加](/engine/6000.0/manual/packages-list/managing-packages-window/upm-ui-actions/upm-ui-quick.md)，即 `com.unity.profiling.core`。

## 性能分析器 Markers##profiler-markers

您可以使用 [`ProfilerMarker` API](/engine/6000.0/script-reference/unity/profiling/profilermarker.md) 标记资源密集型脚本代码块，使它们在 Unity Profiler 中可见，或使用 [Unity Profileling Core](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest) 包中的 [`ProfilerMarker<TP1>` API](https://docs.unity3d.com/Packages/com.unity.profiling.core@1.0/api/Unity.Profiling.ProfilerMarker-1.html) 向其生成的每个样本添加额外的元数据。

这可能很有用，因为内置的 Unity Profiler 不会对All方法Callslls。另一种方法是[使用深度性能分析](/engine/6000.0/manual/analysis/profiler/deep-profiling.md)，但这会导致大量开销，从而显著减慢应用程序执行速度并歪曲结果。使用 `ProfilerMarker` 是标记代码的更有效方法。

`ProfilerMarker` 表示指定的性能分析器手柄，是分析代码的最有效方法。您可以在应用程序程序的Any C# 代码使用它。

当 Unity 将它们部署到非开发版时，性能分析器 Markers 没有开销，因此您可以根据需要标记任意数量的样本。

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

[`ProfilerRecorder`](/engine/6000.0/script-reference/unity/profiling/profilerrecorder.md) API 还使用性能分析器 Markers 来捕获性能数据。在 Unity Editor 和 Development 玩家 中进行Development期间，您可以使用 API 来了解代码的不同部分的性能Overview并识别性能问题。

## 性能分析器 计数器##profiler-counters

要在 Unity Profiler 中显示自定义 Metrics，请使用 [Unity Profileing 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 的Any性能分析类别。有关可用性能分析器类别的完整列表，请参阅[`ProfilerCategory`](/engine/6000.0/script-reference/unity/profiling/profilercategory.md)。

您可以使用 `ProfilerCounter` 或 `ProfilerCounterValue` API 来轨道应用程序的 Metrics，并使它们在 Unity Profiler 或其他代码中可见。如果您是 Asset Store（资源商店） 资源开发者者者，则可以在代码中Add Profiler数器来帮助Other开发者了解System的重要性性能特征，他们可以使用这些信息进行优化或预算工具。

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


**Profiler counters flow.:**
![](/api/media?file=/engine/6000.0/media/images/profiler-counter-diagram.png)

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

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

* int
* long
* float
* 双精度

## 其他资源##additional-resources

* [向代码添加性能分析器 Markers](/engine/6000.0/manual/analysis/profiler/customizing/adding-information-code/add-markers-code.md)
* [向代码中添加性能分析器计数器](/engine/6000.0/manual/analysis/profiler/customizing/adding-information-code/add-counters-code.md)
* [可视化性能分析器计数器](/engine/6000.0/manual/analysis/profiler/customizing/adding-information-code/creating-custom-counters.md)
* API
* [性能分析核心包](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest)
