# 创建性能分析器模块

> Build custom Profiler modules to display performance data and visualizations for your project's specific needs.

性能分析器模块在分析器窗口中显示来自系统的性能数据。

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

* [性能分析器模块编辑器](#Creating-custom-Profiler-module-editor)
* [创建 `ProfilerModule` 脚本](#Creating-custom-Profiler-module-code)。

自定义模块会在性能分析器窗口图表视图 (A) 中显示在代码中指定的计数器，并且这些计数器会以列表形式出现在模块详细信息面板 (B)。


**Frame:**
![](/api/media?file=/engine/6000.0/media/images/Profiler_Tank_module.png)

## 使用性能分析器模块编辑器创建性能分析器模块

您可以使用 Unity 内置的[性能分析器模块编辑器](/engine/6000.0/manual/analysis/profiler/collect-data/visualizing-data/customizing/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/adding-information-code/add-counters-code.md)计数器以添加到模块中。可用计数器列表在**可用计数器**窗格中显示。

要创建自己的模块：

1. 打开性能分析器窗口（**窗口 (Window) > 分析 (Analysis) > 性能分析器 (Profiler)**）。
2. 选择性能分析器模块 (Profiler Modules) 下拉选单，然后选择齿轮图标。此时会打开性能分析器模块编辑器。
3. 选择窗口左下角的**添加**按钮。Unity 会在名为**新的性能分析器模块 (New Profiler Module)** 的列表中添加了一个新的性能分析器模块。要重命名该模块，请单击文本字段并使用键盘设置一个名称。
4. 从**可用计数器 (Available Counters)** 面板中选择要在模块中显示的计数器，然后双击它们以添加到模块中。要选择多个计数器，请按住 Shift 并单击以选择一个范围，或按住 Ctrl（在 macOS 上为 Command 键）以一次选择一个计数器。
5. 要重新排序计数器，可将其进行拖动。一个模块最多只能添加 10 个计数器。
6. 选择性能分析器模块 (Module Editor) 窗口右下角的保存更改 (Save Changes) 按钮。Unity 会关闭该窗口，并且 Unity 会在性能分析器 (Profiler) 窗口中显示新模块。

\*\*重要提示：\*\*如果性能分析器窗口中没有加载任何数据，那么在加载性能分析器模块编辑器时，您创建的所有计数器都不会出现在可用计数器窗格中。要查看您的自定义计数器，您必须使用性能分析器[捕获或加载一些包含您发出的计数器的数据](/engine/6000.0/manual/analysis/profiler/collect-data.md)，然后重新打开性能分析器模块编辑器。


**性能分析器模块编辑器，其中列出了用户计数器。:**
![](/api/media?file=/engine/6000.0/media/images/profiler-module-editor-user-counters.png)

## 在代码中创建性能分析器模块

如要通过代码创建性能分析器模块，必须创建一个新的 `ProfilerModule` 脚本并定义该模块的属性，包括显示的计数器、名称和图标。

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

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

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

2. 将 [`[ProfilerModuleMetadata]`](/engine/6000.0/script-reference/unity-profiling-editor/profiler-module-metadata-attribute.md) 属性分配给该类，并在属性的参数中指定模块的显示名称。在以下示例中，显示名称为“Tank Effects”：

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

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

   ```lang-csharp
   static readonly ProfilerCounterDescriptor[] k_Counters = new ProfilerCounterDescriptor[]
   {
       new ProfilerCounterDescriptor(GameStatistics.TankTrailParticleCountName, GameStatistics.TanksCategory),
       new ProfilerCounterDescriptor(GameStatistics.ShellExplosionParticleCountName, GameStatistics.TanksCategory),
       new ProfilerCounterDescriptor(GameStatistics.TankExplosionParticleCountName, GameStatistics.TanksCategory),
   };
       
   public TankEffectsProfilerModule() : base(k_Counters) { }
   ```

## 在性能分析器窗口中显示自定义的性能分析器模块

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

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

此外，还可以在应用程序处于播放模式时运行性能分析器。但是，如果在播放模式下对应用程序进行性能分析，则性能分析器所显示的数据并不能代表该应用程序在硬件设备上构建运行时的真实情况。

## 其他资源

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