选择模块时,性能分析器模窗口底部将显示模块详细信息面板。您可以对这部分内容进行自定义设置,从而展示与模块相关的更多细节信息,或是实现性能数据的个性化可视化呈现。
要为性能分析器模块创建模块详细信息面板,请执行以下操作:
使用 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 构造函数参数指定 Scripts 和 Memory 类别。当模块处于活动状态时,脚本会启用以下类别:
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}.";
}
}