将性性能分析器数器添加到代码中
使用性性能分析器数器收集指定的统计信息。
阅读时间4 分钟最后更新于 10 天前
要添加性性能分析器数器,请创建脚本来执行以下操作:
这些部分中的代码示例添加了性能分析器计数器来轨道 Unity 为游戏对象的拖尾效果的每个实例创建的粒子的总计数量。在这些示例中,游戏对象的 Name 为“Tank”。
先决条件
一些示例使用性能分析核心包,必须在 Start 之前安装该包。在 Package Manager UI 中找不到 Unity Profiling Core 包,因为它是包。要安装该包,请添加其 Name,即 。
com.unity.profiling.core创建和定义计数器
要创建新建数器:
- 编写一个脚本来定义新建数器的值类型。
- 向此类型分配Name和单位。
性能分析器 计数器 API 支持推送操作。您可以将计数器的值推送性性能分析器,或者性性能分析器可以在帧结束取值。
如果您的数据变化不频繁,例如每帧一次,请使用 API 将计数器数据推送到性能分析器。如果您的数据每帧更改多次,请使用 API。这使得性能性能分析器自动获取帧结束的最后一个值。
ProfilerCounterProfilerCounterValue以下示例脚本定义了具有 Name 和单位的 :
Tank Trail ParticlesProfilerMarkerDataUnit.CountProfilerCounterValueTankTrailParticleCountpublic 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);}
选项 和 会自动将计数器发送到性能分析器数据流,并在帧结束时将数量值重置为零。
FlushOnEndOfFrameResetToZeroOnFlush更新计数器的值
要更更新数器的值,请创建一个 MonoBehaviour 脚本设置已定义的计数器的值。
以下 MonoBehaviour 脚本 更新 函数中计算每一帧属于指定游戏对象对象的拖尾粒子数量。为此,它使用名为 的计数器。
TankTrailParticleCount以下示例脚本还在 Inspector 中创建了名为拖尾粒子系统 () 的 public 属性:
m_TrailParticleSystemusing UnityEngine; 类 TankMovement:MonoBehaviour { public ParticleSystem m_TrailParticleSystem; void Update() { GameStats.TankTrailParticleCount.Value += m_TrailParticleSystem.particleCount; } }
将性能性能分析器数器添加到自定义义性能性能分析器模块
要通过代码在自定义性能分析器模块中显示性能分析器计数器,必须新建 脚本并定义模块的 Properties,包括其显示的计数器、Name 和图标。ON 有关如何执行此操作的信息,请参阅在代码中创建性能分析器模块。
ProfilerModule