VisualElement 返回视图控制器的视图。VisualElement。
创建视图控制器的视图。当 Unity 即将首次显示视图控制器的视图时,会自动调用该方法。
您必须重写该方法才能构建视图控制器的视图层级。为此,请从 UXML 文件 加载可视化树,或者按照示例代码所示直接在 C# 中构建视图。
using Unity.Profiling.Editor; using UnityEditor; using UnityEngine.UIElements;
public class SingleCounterViewController : ProfilerModuleViewController { ProfilerCounterDescriptor m_Counter; Label m_CounterLabel;
public SingleCounterViewController(ProfilerWindow profilerWindow, ProfilerCounterDescriptor counter) : base(profilerWindow) { m_Counter = counter; }
protected override VisualElement CreateView() { // Create a simple view with a single label. var view = new VisualElement(); m_CounterLabel = new Label(); view.Add(m_CounterLabel);
// Subscribe to Profiler window SelectedFrameIndexChanged event. ProfilerWindow.SelectedFrameIndexChanged += OnSelectedFrameIndexChanged;
// Populate label with counter value in selected frame. ReloadData();
return view; }
protected override void Dispose(bool disposing) { if (!disposing) return;
// Unsubscribe from Profiler window SelectedFrameIndexChanged event. ProfilerWindow.SelectedFrameIndexChanged -= OnSelectedFrameIndexChanged;
base.Dispose(disposing); }
void OnSelectedFrameIndexChanged(long selectedFrame) { // Update label with counter value in selected frame. ReloadData(); }
void ReloadData() { // Update label text with formatted counter value in selected frame. var selectedFrameIndexInt32 = System.Convert.ToInt32(ProfilerWindow.selectedFrameIndex); var formattedCounterValue = UnityEditorInternal.ProfilerDriver.GetFormattedCounterValue(selectedFrameIndexInt32, m_Counter.CategoryName, m_Counter.Name); m_CounterLabel.text = $"{m_Counter}: {formattedCounterValue}"; } }