Version: 6000.3
语言: 中文
创建探查器模块
Profiler 模块编辑器参考

创建Profiler模块详细信息面板

选择模块时,模块详细信息面板将显示在 Profiler 窗口的底部。您可以自定义此部分以显示与模块相关的其他详细信息或显示性能数据的自定义可视化。

要为您的模块创建模块详细信息面板分析器帮助您优化游戏的窗口。它显示了在游戏的各个领域花费了多少时间。例如,它可以报告渲染、动画制作或游戏逻辑所花费的时间百分比。更多信息
请参阅术语表
模块:

创建脚本以控制模块详细信息面板

使用ProfilerModuleViewController基类,以自定义 性能分析器(Profiler) 窗口中的模块详细信息面板。为此,请创建一个脚本来控制选择特定模块时模块详细信息面板中显示的内容。

脚本必须执行以下作:

  • 为调用基构造函数的视图控制器定义公共构造函数base(profilerWindow).
  • 覆盖CreateView以构建自定义模块详细信息面板。

例如:


 public class CustomDetailsViewController : ProfilerModuleViewController
 {   
    public CustomDetailsViewController(ProfilerWindow profilerWindow) : base(profilerWindow) { }

    protected override VisualElement CreateView()
    {
        // Create your UI.
    }
}

模块详细信息面板控制器脚本示例

以下示例脚本创建一个模块详细信息面板控制器,该控制器在模块详细信息面板中绘制一个显示文本的标签:

自定义性能分析器模块,在模块详细信息面板中显示自定义消息
自定义性能分析器模块,在模块详细信息面板中显示自定义消息

脚本示例执行以下作:

  • 定义并创建一个标签以显示要捕获的值,并将此标签添加到模块详细信息面板。
  • 定义一个构造函数来控制模块详细信息面板,并使用CreateView以构建自定义模块详细信息面板。
  • 使用当前帧中的数据填充标签,并在每一帧后更新标签。
  • 将计数器值提取为字符串,你可以在模块详细信息面板中显示该值。
  • 指定要在模块详细信息面板中显示的文本,并告诉性能器每帧自动更新它。
 using UnityEditor;
 using UnityEditorInternal;
 using Unity.Profiling.Editor;
 using UnityEngine.UIElements;
 
 public class TankEffectsDetailsViewController : ProfilerModuleViewController
 {
    // Define a label, which will display the total particle count for tank trails in the selected frame.
    Label m_TankTrailParticleCountLabel;

    // Define a constructor for the view controller, which calls the base constructor with the Profiler Window passed from the module.
    public TankEffectsDetailsViewController(ProfilerWindow profilerWindow) : base(profilerWindow) { }
    {
        var view = CreateView();

        // Populate the view with the current data for the selected frame. 
        ReloadData();

        // Be notified when the selected frame index in the Profiler Window changes, so we can update the label.
        ProfilerWindow.SelectedFrameIndexChanged += OnSelectedFrameIndexChanged;

        return view;
    }
    
    protected virtual VisualElement CreateView()
    {
        var view = new VisualElement();
        
        // Create the label and add it to the view.
        m_TankTrailParticleCountLabel = new Label() { style = { paddingTop = 8, paddingLeft = 8 } };
        view.Add(m_TankTrailParticleCountLabel);
    }
    

    // Override Dispose to do any cleanup of the view when it is destroyed. This is a standard C# Dispose pattern.
    protected override void Dispose(bool disposing)
    {
        if (!disposing)
            return;

        // Unsubscribe from the Profiler window event that we previously subscribed to.
        ProfilerWindow.SelectedFrameIndexChanged -= OnSelectedFrameIndexChanged;

        base.Dispose(disposing);
    }

    protected virtual void ReloadData()
    {
        // Retrieve the TankTrailParticleCount counter value from the Profiler as a formatted string.
        var selectedFrameIndexInt32 = System.Convert.ToInt32(ProfilerWindow.selectedFrameIndex);
        var value = ProfilerDriver.GetFormattedCounterValue(selectedFrameIndexInt32, GameStatistics.TanksCategory.Name, GameStatistics.TankTrailParticleCountName);

        // Update the label's text with the value.
        m_TankTrailParticleCountLabel.text = $"The value of '{GameStatistics.TankTrailParticleCountName}' in the selected frame is {value}.";
    }

    void OnSelectedFrameIndexChanged(long selectedFrameIndex)
    {
        // Update the label with the current data for the newly selected frame.
        ReloadData();
    }
}

提示: 您可以使用 Unity 的 UI 工具包为模块详细信息面板构建自定义 UI。有关更多信息,请参阅 UI 工具包

以下示例图像显示了属于自定义自适应性能模块的自定义模块详细信息面板:

具有自定义 UI 可视化效果的自定义 Profiler 模块。
具有自定义 UI 可视化效果的自定义 Profiler 模块。

将自定义模块详细信息面板连接到性能分析器模块

要显示自定义模块详细信息面板,你需要在选择分析器模块时实例化模块详细信息面板控制器。为此,请覆盖CreateDetailsViewController以创建和绘制新的模块详细信息面板控制器。然后,Unity 在显示模块的详细信息面板时调用此方法。

以下代码示例为名为TankEffectsProfilerModule:


 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),
    };

    public TankEffectsProfilerModule() : base(k_Counters) { }

    public override ProfilerModuleViewController CreateDetailsViewController()
    {
        return new TankEffectsDetailsViewController(ProfilerWindow);
    }
}

在模块详细信息面板中可视化其他计数器

您可以显示其他分析器计数器使用 ProfilerCounter API 放置在代码中,以跟踪指标,例如游戏中生成的敌人数量。更多信息
请参阅术语表
未包含在模块的图表视图中。当您想要显示所选帧的其他数据时,这非常有用。

当模块处于活动状态时,Profiler 会自动捕获属于模块图表视图的所有计数器的类别。要捕获其他计数器,请编写一个脚本,以告知探查器在模块处于活动状态时捕获特定类别。

例如,以下脚本使用autoEnabledCategoryNames构造函数参数来指定ScriptsMemory类别。当模块处于活动状态时,脚本会启用以下类别:

 
using Unity.Profiling;
using Unity.Profiling.Editor;

[System.Serializable]
[ProfilerModuleMetadata("Tank Effects & Memory")]
public class TankEffectsAndMemoryProfilerModule : ProfilerModule
{
   static readonly ProfilerCounterDescriptor[] k_Counters = new ProfilerCounterDescriptor[]
   {
       new ProfilerCounterDescriptor(GameStatistics.TankTrailParticleCountName, ProfilerCategory.Scripts),
       new ProfilerCounterDescriptor(GameStatistics.ShellExplosionParticleCountName, ProfilerCategory.Scripts),
       new ProfilerCounterDescriptor(GameStatistics.TankExplosionParticleCountName, ProfilerCategory.Scripts),
   };

   // Enable the ProfilerCategory.Scripts and ProfilerCategory.Memory categories when the module is active.
   static readonly string[] k_AutoEnabledCategoryNames = new string[]
   {
       ProfilerCategory.Scripts.Name,
       ProfilerCategory.Memory.Name
   };

   public override ProfilerModuleViewController CreateDetailsViewController()
   {
       return new TankEffectsAndMemoryDetailsViewController(ProfilerWindow);
   }
   
   // Pass the auto-enabled category names to the base constructor.
   public TankEffectsProfilerModule() : base(k_Counters, autoEnabledCategoryNames: k_AutoEnabledCategoryNames) { }
}

以下示例代码显示内置的 Mesh Memory 探查器计数器TankTrailParticleCount:

 using UnityEditor;
 using UnityEditorInternal;
 using Unity.Profiling.Editor;
 using UnityEngine.UIElements;
 
 public class TankEffectsAndMemoryDetailsViewController : TankEffectsDetailsViewController
 {
    // Define a label, which will display the total mesh memory in the selected frame
    Label m_MeshMemoryLabel;
    
    protected override VisualElement CreateView()
    {
        var view = base.CreateView();
        
        // Create the label and add it to the view.
        m_MeshMemoryLabel = new Label() { style = { paddingTop = 8, paddingLeft = 8 } };
        view.Add(m_MeshMemoryLabel);
    }
    
    protected override void ReloadData()
    {
        base.ReloadData();
        
        // Retrieve the Mesh Memory counter value from the Profiler as a formatted string.
        var selectedFrameIndexInt32 = System.Convert.ToInt32(ProfilerWindow.selectedFrameIndex);
        var value = ProfilerDriver.GetFormattedCounterValue(selectedFrameIndexInt32, ProfilerArea.Memory, "Mesh Memory");

        // Update the label's text with the value.
        m_MeshMemoryLabel.text = $"The value of 'Mesh Memory' in the selected frame is {value}.";
    }
}

其他资源

创建探查器模块
Profiler 模块编辑器参考