Version: 2022.2

ProfilerMarker

struct in Unity.Profiling

切换到手册

描述

用于分析任意代码块的性能标记。

使用 ProfilerMarker 标记 Profiler 的脚本代码块。\ 由标记生成的信息显示在 CPU Profiler 中,也可以使用 Recorder 捕获。在开发期间(在 Editor 和 Development Player 中),这有助于获得游戏代码的不同部分的性能概况并识别性能问题。

using Unity.Profiling;

public class MySystemClass { static readonly ProfilerMarker s_PreparePerfMarker = new ProfilerMarker("MySystem.Prepare"); static readonly ProfilerMarker s_SimulatePerfMarker = new ProfilerMarker(ProfilerCategory.Ai, "MySystem.Simulate");

public void UpdateLogic() { s_PreparePerfMarker.Begin(); // ... s_PreparePerfMarker.End();

using (s_SimulatePerfMarker.Auto()) { // ... } } }

ProfilerMarker represents a named profiler handle and is the most efficient way of profiling your code. It can be used in jobified code.
Methods Begin and End are marked with ConditionalAttribute. They are conditionally compiled away and thus have zero overhead in non-Development (Release) builds.

当 Profiler 收集检测数据时,ProfilerMarker 有助于减少开销和传输的数据量。Profiler.BeginSample 将完整字符串传输到数据流,而 ProfilerMarker.BeginCustomSampler.Begin 仅传输标记的整数标识符。

另外 ProfilerMarker.EndRecorder 提供上下文信息,因此可以在播放器中跟踪标记代码的时序。

另请参阅:Recorder

变量

HandleGets native handle of the ProfilerMarker.

构造函数

ProfilerMarker为代码检测构造一个新的性能标记。

公共函数

Auto为作用域 **using** 块创建一个 helper 结构。
Begin开始分析标有此 ProfilerMarker 实例所定义的自定义名称的一段代码。
End结束分析标有此 ProfilerMarker 实例所定义的自定义名称的一段代码。