Version: Unity 6.0 (6000.0)
语言 : 中文
Create a Profiler module
性能分析器模块编辑器参考

创建性能分析器模块详细信息面板

选择模块时,性能分析器模窗口底部将显示模块详细信息面板。您可以对这部分内容进行自定义设置,从而展示与模块相关的更多细节信息,或是实现性能数据的个性化可视化呈现。

要为性能分析器模块创建模块详细信息面板,请执行以下操作:

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

使用 ProfilerModuleViewController 基类可在性能分析器窗口中自定义模块详细信息面板。若要实现自定义,需创建一个脚本,用于控制在选择特定模块时,模块详细信息面板中显示的内容。

该脚本必须执行以下操作:

  • 为视图控制器定义一个公共构造函数,此构造函数需调用基类构造函数 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__(即用户界面,User Interface)让用户能够与您的应用程序进行交互。Unity 目前支持三种 UI 系统。更多信息
See in Glossary
工具包为模块详细信息面板构建自定义 UI。有关更多信息,请参阅 UI 工具包

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

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

要显示自定义模块详细信息面板,需要在选择性能分析器模块时实例化模块详细信息面板控制器。如要执行此操作,请覆盖 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);
    }
}

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

您可以显示模块的图表视图中未包含的其他性能分析器计数器。如果要显示所选帧的其他数据,此功能很实用。

当模块处于活动状态时,性能分析器会自动捕获属于该模块图表视图的所有计数器的类别。要捕获其他计数器,请编写脚本,让性能分析器在模块处于活动状态时捕获特定类别。

例如,以下脚本使用 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) { }
}

以下示例代码在 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}.";
    }
}

其他资源

Create a Profiler module
性能分析器模块编辑器参考
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961