---
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/add-counters-code
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/analysis/profiler/customizing/adding-information-code/add-counters-code.md
last_updated: 2026-10-30
generated_at: 2026-11-20T21:04:28.046Z
locale: zh-cn
---

# 将性性能分析器数器添加到代码中

> 使用性性能分析器数器收集指定的统计信息。

要添加性性能分析器数器，请创建脚本来执行以下操作：

* [创建和定义新建计数器](#creating-and-defining-a-counter)。
* [更新计数器的值](#updating-a-counters-value)。

这些部分中的代码示例添加了性能分析器计数器来轨道 Unity 为游戏对象的拖尾效果的每个实例创建的粒子的总计数量。在这些示例中，游戏对象的 Name 为“Tank”。

## 先决条件##prerequisites

一些示例使用[性能分析核心包](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest)，必须在 Start 之前安装该包。在 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`。

## 创建和定义计数器##creating-and-defining-a-counter

要创建新建数器：

1. 编写一个脚本来定义新建数器的值类型。
2. 向此类型分配Name和单位。

> **Important:**
>
> 创建计数器When，必须指定新建数器所属的性能分析性能分析器Category。为此，请使用 [`ProfilerCategory`](/engine/6000.0/script-reference/unity/profiling/profilercategory.md) 类中的现有 Unity Category。以下示例中的脚本使用现有的 `ProfilerCategory.Scripts` Category。

性能分析器 计数器 API 支持推送操作。您可以将计数器的值推送性性能分析器，或者性性能分析器可以在帧结束取值。

如果您的数据变化不频繁，例如每帧一次，请使用[`ProfilerCounter`](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest?subfolder=/api/Unity.Profiling.ProfilerCounter-1.html) API 将计数器数据推送到性能分析器。如果您的数据每帧更改多次，请使用`ProfilerCounterValue` API。这使得性能性能分析器自动获取帧结束的最后一个值。

以下示例脚本定义了具有 Name `Tank Trail Particles` 和[`ProfilerMarkerDataUnit.Count`](/engine/6000.0/script-reference/unity/profiling/profilermarkerdataunit/count.md)单位的 [`ProfilerCounterValue`](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest?subfolder=/api/Unity.Profiling.ProfilerCounter-1.html) `TankTrailParticleCount`：

```lang-cs
public static class GameStats
{
   public static readonly ProfilerCategory TanksCategory = ProfilerCategory.Scripts;

   public const string TankTrailParticleCountName = "Tank Trail Particles";
   public static readonly ProfilerCounterValue<int> TankTrailParticleCount =
       新建 ProfilerCounterValue<int>(TanksCategory、TanksTrailParticleCountName、ProfilerMarkerDataUnit.数量、
           ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
}
```

选项 `FlushOnEndOfFrame` 和 `ResetToZeroOnFlush` 会自动将计数器发送到性能分析器数据流，并在帧结束时将数量值重置为零。

## 更新计数器的值##updating-a-counter's-value

要更更新数器的值，请创建一个 MonoBehaviour 脚本设置已定义的计数器的值。

以下 MonoBehaviour 脚本 更新 函数中计算每一帧属于指定游戏对象对象的拖尾粒子数量。为此，它使用名为 `TankTrailParticleCount` 的计数器。

以下示例脚本还在 Inspector 中创建了名为拖尾粒子系统 (`m_TrailParticleSystem`) 的 public 属性：

```lang-cs
 using UnityEngine;

 类 TankMovement：MonoBehaviour
 {
    public ParticleSystem m_TrailParticleSystem;

    void Update()
    {
        GameStats.TankTrailParticleCount.Value += m_TrailParticleSystem.particleCount;
    }
 }
```

### 将性能性能分析器数器添加到自定义义性能性能分析器模块##adding-profiler-counters-to-a-custom-profiler-module

要通过代码在自定义性能分析器模块中显示性能分析器计数器，必须新建 `ProfilerModule` 脚本并定义模块的 Properties，包括其显示的计数器、Name 和图标。ON 有关如何执行此操作的信息，请参阅[在代码中创建](/engine/6000.0/manual/analysis/profiler/customizing/creating-custom-modules/create-modules.md)性能分析器模块。

## 其他资源##additional-resources

* [在代码简介中添加性能分析信息](/engine/6000.0/manual/analysis/profiler/customizing/adding-information-code/intro.md)
* [向代码添加性能分析器 Markers](/engine/6000.0/manual/analysis/profiler/customizing/adding-information-code/add-markers-code.md)
* [自定义性能分析器模块](/engine/6000.0/manual/analysis/profiler/customizing.md)
* [性能分析核心包](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest)
