Unity3D:创建自定义模块详细信息面板

推荐:将NSDT场景编辑器加入你的3D工具链
3D工具集:NSDT简石数字孪生
创建自定义模块详细信息面板
选择模块时,模块详细信息面板将显示在“性能分析器”窗口的底部。您可以自定义此部分以显示与模块相关的其他详细信息或显示性能数据的自定义可视化效果。
要为 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) { }
// Override CreateView to build the custom module details panel.6666666667reateView()
{
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);
// Populate the label 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;
}
// 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);
}
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();
}
}
在模块详细信息面板中创建自定义 UI 元素
您可以使用 Unity 的 UIToolkit 为模块详细信息面板构建自定义 UI。有关详细信息,请参阅 UI 工具包。
以下示例图像显示了属于自定义自适应性能模块的自定义模块详细信息面板:

将自定义模块详细信息面板连接到性能分析器模块
若要显示自定义模块详细信息面板,需要在选择 Profiler 模块时实例化模块详细信息面板控制器。为此,请覆盖以创建和绘制新的模块详细信息面板控制器。然后,Unity 在显示模块的详细信息面板时调用此方法。CreateDetailsViewController
下面的代码示例实例化名为 的模块的自定义模块详细信息面板: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
using Unity.Profiling;
using Unity.Profiling.Editor;
[System.Serializable]
[ProfilerModuleMetadata("Tank Effects")]
public class TankEffectsProfilerModule : 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
};
// Pass the auto-enabled category names to the base constructor.
public TankEffectsProfilerModule() : base(k_Counters, autoEnabledCategoryNames: k_AutoEnabledCategoryNames) { }
此文由3D建模学习工作室整理翻译,转载请注明出处!