Unity3D:创建自定义探查器计数器

推荐:将NSDT场景编辑器加入你的3D工具链
3D工具集:NSDT简石数字孪生
创建自定义探查器计数器
要在 Unity 性能分析器中显示自定义指标,必须在 Unity 性能分析核心包中使用性能分析器计数器 API。
可以使用分析核心 API 跟踪应用程序中的指标。您可以在 Unity 性能分析器中显示计数器跟踪的信息。使用自定义探查器计数器比较系统指标,并在“探查器”窗口中识别性能问题。
自定义探查器计数器可以显示来自 或 的数据。ProfilerCounterProfilerCounterValue
有关使用 Unity 性能分析核心 API 创建性能分析器计数器的完整指南,请参阅性能分析器计数器 API 指南。
若要添加探查器计数器,请创建脚本以执行以下操作:
- 创建新计数器。
- 将计数器分配给探查器类别。
- 更新计数器的值。
这些部分中的代码示例添加了一个性能分析器计数器,用于跟踪 Unity 为游戏对象的轨迹效果的每个实例创建的粒子总数。在这些示例中,游戏对象的名称为“坦克”。
定义计数器
若要创建新计数器,请编写脚本来定义新计数器的值类型,并为此类型分配名称和单位。
创建计数器时,必须指定新计数器所属的探查器类别。为此,请使用现有的 Unity 类别。例如,下面的脚本示例使用现有类别。有关更多信息,请参见使用探查器类别ProfilerCategory.Scripts
以下示例脚本定义了 ProfilerCounterValue ,名称为 “Tank Trail Particles”。此计数器的单位为“计数”:TankTrailParticleCount
public static class GameStats
{
public static readonly ProfilerCategory TanksCategory = ProfilerCategory.Scripts;
public const string TankTrailParticleCountName = "Tank Trail Particles";
public static readonly ProfilerCounterValue<int> TankTrailParticleCount =
new ProfilerCounterValue<int>(TanksCategory, TankTrailParticleCountName, ProfilerMarkerDataUnit.Count,
ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
}
选项,并自动将计数器发送到 Profiler 数据流,并在帧结束时将 Count 值重置为零。FlushOnEndOfFrameResetToZeroOnFlush
使用探查器类别
Unity 根据计数器分析的工作类型(例如渲染、脚本或动画)自动将性能分析器计数器分组到类别中。您可以将自定义性能分析器计数器分配给 Unity 的任何性能分析类别。有关可用探查器类别的完整列表,请参阅探查器类别。
将探查器计数器分配给探查器类别
探查器计数器必须属于探查器类别。定义计数器时,应为探查器计数器分配类别。为此,请使用探查器模块的可选构造函数参数将一个或多个类别分配给探查器计数器。以下示例代码中有一个此方法的示例:autoEnabledCategoryNames
using Unity.Profiling;
using Unity.Profiling.Editor;
[System.Serializable]
[ProfilerModuleMetadata("Tank Effects")]
public class TankEffectsProfilerModule : ProfilerModule
{
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),
};
// Ensure that both ProfilerCategory.Scripts and ProfilerCategory.Memory categories are enabled when our module is active.
static readonly string[] k_AutoEnabledCategoryNames = new string[]
{
ProfilerCategory.Scripts.Name,
ProfilerCategory.Memory.Name
};
// Pass the auto-enabled category names to the base constructor.
public TankEffectsProfilerModule() : base(k_Counters, autoEnabledCategoryNames: k_AutoEnabledCategoryNames) { }
}
更新计数器的值
要更新计数器的值,请创建一个 MonoBehavior 脚本来设置已定义的计数器的值。有关详细信息,请参阅如何将计数器值传递给探查器。
更新计数器的值脚本示例
此示例 MonoBehavior 脚本在更新函数中的每一帧中计算属于指定游戏对象的尾迹粒子数。为此,它使用名为 的计数器。TankTrailParticleCount
以下示例脚本还会在检查器中创建一个名为“轨迹粒子系统 ()”的公共属性:m_TrailParticleSystem
using UnityEngine;
class TankMovement : MonoBehaviour
{
public ParticleSystem m_TrailParticleSystem;
void Update()
{
GameStats.TankTrailParticleCount.Value += m_TrailParticleSystem.particleCount;
}
}

使用探查器计数器分析发布版本
在发布播放器中运行项目时,无权访问“性能分析器”窗口。但是,您可以将计数器显示为发布播放器中的 UI 元素。这意味着您可以在已发布的应用程序中包括分析工具。为此,请参阅探查器计数器 API 指南中的获取玩家中的计数器值。
下图显示了在发布播放器中使用自定义 UI 的场景左上角的计数器:

此文由3D建模学习工作室整理翻译,转载请注明出处!