包含此页的版本:
不含此页的版本:
选择模块时,模块详细信息面板将显示在 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 工具包。
以下示例图像显示了属于自定义自适应性能模块的自定义模块详细信息面板:
要显示自定义模块详细信息面板,你需要在选择分析器模块时实例化模块详细信息面板控制器。为此,请覆盖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构造函数参数来指定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) { }
}
以下示例代码显示内置的 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}.";
}
}