Version: 6000.3
语言: 中文
将分析信息添加到代码简介中
将探查器计数器添加到代码中

将探查器标记添加到代码

探查器标记放置在代码中,用于描述 CPU 或 GPU 事件,然后显示在 Unity 探查器窗口中。默认添加到 Unity 代码中,或者您可以使用 ProfilerMarker API 添加自己的自定义标记。更多信息
请参阅术语表
到您的代码中查看示例ProfilerMarker.Begin,ProfilerMarker.EndProfilerMarker.Auto在 性能分析器(Profiler) 窗口CPU 使用率(CPU Usage) 模块的时间轴视图(Timeline View) 和 层次结构视图(Hierarchy View) 中生成:

在时间轴视图中包含元数据的探查器示例。
在时间轴视图中包含元数据的探查器示例。
在层次结构视图中包含元数据的探查器示例。
在层次结构视图中包含元数据的探查器示例。

先决条件

一些示例使用 Profiling Core 包,您必须在开始之前安装该包。Unity Profiling Core 包无法在包管理器 UI 中发现,因为它是核心包。要安装包,请按其名称添加它,即com.unity.profiling.core.

标记代码

要使用ProfilerMarker应用程序接口,在调用之间放置要分析的代码ProfilerMarker.BeginProfilerMarker.End.例如:

using UnityEngine;
using Unity.Profiling;

public class ProfilerMarkerExample
{
    static readonly ProfilerMarker k_MyCodeMarker = new ProfilerMarker("My Code");

    void Update() {
        k_MyCodeMarker.Begin();
        Debug.Log("This code is being profiled");
        k_MyCodeMarker.End();
    }
}

注意:避免在/分析器帮助您优化游戏的窗口。它显示了在游戏的各个领域花费了多少时间。例如,它可以报告渲染、动画制作或游戏逻辑所花费的时间百分比。更多信息
请参阅术语表
标记的名称,因为这会使 Profiler 窗口无法在 CPU Profiler 模块的图表视图中突出显示标记。

确保BeginEnd调用之前不会退出作用域End被称为。如果代码在之前退出作用域End调用时,错误消息将记录到控制台。为避免必须调用End每次返回之前,使用Auto以便样品在离开示波器时自动结束。有关详细信息,请参阅本文档中有关自动关闭探查器标记代码块的部分。

Unity 记录分析代码块的执行时间并将其报告给 Profiler,并将其显示在 CPU Profiler 模块中,而无需使用深度分析。它将其显示为 CPU Profiler 模块的层次结构视图中的新条目,如下所示:

Profiler 示例。
Profiler 示例。

自动关闭分析器标记代码块

ProfilerMarker.Auto以确保ProfilerMarker.End在代码块末尾自动调用。以下调用是等效的:

using Unity.Profiling;

public class MySystemClass
{
    static readonly ProfilerMarker k_UpdatePerfMarker = new ProfilerMarker("MySystem.Update");

    public void Update()
    {
        k_UpdatePerfMarker.Begin();
        // ...
        k_UpdatePerfMarker.End();

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

Begin()End()调用时,Unity 无法编译出ProfilerMarker.Auto调用非开发(发布)版本。但是,它将返回 null,这只会增加最小的开销。

您还可以使用ProfilerMarker.Auto使用using varEnd当前作用域结束后,调用会自动发生。此方法可最大程度地减少添加ProfilerMarker实例添加到您的代码中:

using Unity.Profiling;

public class MySystemClass
{
    static readonly ProfilerMarker k_UpdatePerfMarker = new ProfilerMarker("MySystem.Update");

    public void Update()
    {
        using var _ = k_UpdatePerfMarker.Auto();
        // ...
    }
}

注意:任何异步await和任何yield在标记为ProfilerMarker的,并且会在控制台中记录错误消息,即使您使用Auto.

向样本添加整数或浮点参数

有时,您可能希望向代码示例添加上下文,以确定代码长时间运行的特定条件。

例如,如果您的系统对对象进行模拟,则可以使用探查器示例与探查器标记关联的一组数据,探查器已记录和收集这些数据。
请参阅术语表
.如果探查器返回异常数字和较长的采样持续时间,则可能意味着您必须使用另一个线程进行模拟,将 CPU 工作拆分为多个帧(时间切片),或调整应用程序的设计以防止丢帧。

ProfilerMarker最多支持三个数字参数:ProfilerMarker<TP1>,ProfilerMarker<TP1, TP2>ProfilerMarker<TP1, TP2, TP3>:

using Unity.Profiling;

public class MySystemClass
{
  static readonly ProfilerMarker<int> k_PreparePerfMarker = new ProfilerMarker<int>("MySystem.Prepare", "Objects Count");
  static readonly ProfilerMarker<float> k_SimulatePerfMarker = new ProfilerMarker<float>(ProfilerCategory.Scripts, "MySystem.Simulate", "Objects Density");

  public void Update(int objectsCount)
  {
    k_PreparePerfMarker.Begin(objectsCount);
    // ...
    k_PreparePerfMarker.End();

    using (k_SimulatePerfMarker.Auto(objectsCount * 1.0f))
    {
      // ...
    }
  }
}

向样本添加字符串参数

ProfilerMarkerAPI 支持将字符串参数添加到探查器标记。如果要在应用程序加载关卡或数据文件时显示关卡或文件的名称,则字符串参数可能很有用。用ProfilerMarkerExtension将字符串参数与 Profiler 示例一起传递的方法:

using Unity.Profiling;

public class MySystemClass
{
  static readonly ProfilerMarker k_PreparePerfMarker = new ProfilerMarker("MySystem.Prepare");

  public void Prepare(string path)
  {
    k_PreparePerfMarker.Begin(path);
    // ...
    k_PreparePerfMarker.End();
  }
}

其他资源

将分析信息添加到代码简介中
将探查器计数器添加到代码中