---
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/creating-custom-modules/create-modules
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/analysis/profiler/customizing/creating-custom-modules/create-modules.md
last_updated: 2026-10-30
generated_at: 2026-11-16T21:07:11.855Z
locale: zh-cn
---

# 创建性能分析器模块

> 将自定义定义性能性能分析器模块添加到 性能分析器 窗口可收集和显示有关应用程序程序的指定的数据。

性能分析器模块在 性能分析器 窗口中显示System的性能数据。

您可以通过以下方式之一创建自己的性能分析器模块：

* [使用性能分析器模块 Editor](#Creating-custom-Profiler-module-editor)。
* 创建 [](#creating-profiler-modules-code) 脚本

自定义定义模块在 性能分析器 窗口图表View (A) 中显示您在代码中指定的计数器，而计数器在模块细节信息面板 (B) 中显示为列表。


**A custom Profiler module with custom data displayed.:**
![](/api/media?file=/engine/6000.0/media/images/Profiler_Tank_module.png)

## 使用 性能分析器模块 Editor 创建 性能分析器模块##create-a-profiler-module-with-the-profiler-module-editor

您可以使用 Unity 的内置[性能分析器模块 Editor](/engine/6000.0/manual/analysis/profiler/customizing/creating-custom-modules/module-editor.md) 来创建自己的性能分析器模块。要在性能分析器模块中收集数据，必须为要轨道的模块添加至少一个计数器。您可以添加两个内置 Unity 计数器，或使用[`ProfilerCounter` API](https://docs.unity3d.com/Packages/com.unity.profiling.core@latest?subfolder=/api/Unity.Profiling.ProfilerCounter-1.html) [创建自己的计数](/engine/6000.0/manual/analysis/profiler/customizing/adding-information-code/add-counters-code.md)器以添加到模块。可用计数器列表显示在 **Available Counters** 面板中。

要创建自己的模块：

1. 打开性能分析器窗口（**窗口 > Analysis > Performance Analytics**）。
2. 选择 性能分析器 模块 Dropdown选单，然后选择齿轮图标。此时将打开 性能分析器模块 Editor。
3. 选择窗口左下角的**添加**按钮。然后 Unity 将新建性能分析器模块添加到列表中，称为\*\*新建性能分析器模块。\*\*重命名模块，请单击Text Field并使用键盘设置Name。
4. 从 **Available Counters** 面板选择要在模块中显示的计数器，然后双击 ON 将它们添加到模块中。要选择多个个计数器，请按住 Shift 并单击以选择一个范围，或按住 Ctrl（macOS ON的 Command）以一时间选择一个计数器。
5. 要重新排序计数器，请Drag它们。一个模块最多只能添加 10 个计数器。
6. 选择 性能分析器模块 Editor 窗口右底部的 Save Changes 按钮。Unity 关闭窗口，Unity 在 性能分析器 窗口中显示新建模块。

> **Important:**
>
> 如果未将Any数据加载到 性能分析器 窗口中，则在加载 性能分析器模块 Editor When，已创建的Any计数器都不会显示在 Available Counters 面板中。要查看自定义计数器，请[捕获或加载一些数据](/engine/6000.0/manual/profiler-collect-data.md)，其中使用性能分析器发送计数器，然后重新打开性能分析器模块 Editor。

{/* 缺少图像：../uploads/Main/profiler-module-editor-user-counters.png（需要作者审查）*/}

## 在代码中创建 性能分析器模块##create-a-profiler-module-in-code

要通过代码创建性能分析器模块，必须新建`ProfilerModule`脚本并定义模块的 Properties，包括其显示的计数器、Name 和图标。

要定义性能分析器模块，脚本必须执行以下操作：

1. 定义从 Project 或包中的[`ProfilerModule`](/engine/6000.0/script-reference/unity/profiling/editor/profilermodule.md)派生的类。在以下示例中，该类称为 `TankEffectsProfilerModule`：

   ```lang-cs
   public class TankEffectsProfilerModule : ProfilerModule
   ```

2. 将 [`[ProfilerModuleMetadata]`](/engine/6000.0/script-reference/unity/profiling/editor/profilermodulemetadataattribute.md) 属性分配给该类并在属性的参数中指定模块的 Display Name。在以下示例中，Display Name为“坦克特效”：

   ```lang-cs
   [ProfilerModuleMetadata("Tank Effects")] 
   ```

3. 实现一个没有参数的构造函数函数，并将图表计数器Descriptions的列表通道给底部构造函数。在以下示例中，没有参数的构造函数为 `TankEffectsProfilerModule()`，图表计数器描述符列表为 `k_Counters`，底部构造函数为 `base`：

   ```lang-cs
   Static 只读 ProfilerCounterDescriptor[] k_Counters = 新建 ProfilerCounterDescriptor[]
   {
       新建 ProfilerCounterDescriptor(GameStatistics.TankTrailParticleCountName, GameStatistics.TanksCategory),
       新建 ProfilerCounterDescriptor(GameStatistics.ShellExplosionParticleCountName, GameStatistics.TanksCategory),
       新建 ProfilerCounterDescriptor(GameStatistics.TankExplosionParticleCountName, GameStatistics.TanksCategory),
   };
       
   public TankEffectsProfilerModule() : base(k_Counters) { }
   ```

## 在 性能分析器 窗口中显示自定义定义 配置文件 模块##display-a-custom-profile-module-in-the-profiler-window

定义自己的性能分析性能分析器模块When，性能分析器 窗口会自动检测该模块。要在 性能分析器 窗口中View 性能分析器模块中的数据，请执行以下操作：

1. 将性能性能分析器连接到应用程序程序。有关更多信息，请参阅[分析应用程序](/engine/6000.0/manual/analysis/profiler/profiling-applications.md)。
2. 运行应用程序。

还可以在应用程序程序处于播放模式When运行性能性能分析器。但是，如果在播放模式下对应用程序程序进行性能配置文件，则性能分析器显示的数据不能代表ON硬件设备上版本应用程序程序When的运行方式。

## 其他资源##additional-resources

* [收集性能数据](/engine/6000.0/manual/analysis/profiler/profiling-applications/profiling-collect-data-introduction.md)
* [性能分析器模块简介](/engine/6000.0/manual/analysis/profiler/visualizing-data/modules-introduction.md)
* [性能分析器模块 Editor 参考](/engine/6000.0/manual/analysis/profiler/customizing/creating-custom-modules/module-editor.md)
