版本: Unity 6 (6000.0)
语言英语
  • C#

ProfilerModuleViewController

Unity.Profiling.Editor 中的类

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实阅读了用户提出的每个建议更改,并在适用的情况下进行更新。

关闭

提交失败

由于某些原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

描述

为 Profiler 窗口中显示的 ProfilerModule 提供单个内容视图。

使用此类在 ProfilerModule 中执行自定义绘制,例如绘制自定义详细信息视图。

要构建模块的视图层次结构,请定义一个派生自 ProfilerModuleViewController 的类型,并重写其 ProfilerModuleViewController.CreateView 方法。要对视图控制器进行任何必要的清理,请重写 ProfilerModuleViewController.Dispose 方法。您无需在 Dispose 过程中显式地从层次结构中删除视图控制器的视图;Unity 会在它处理视图控制器时自动执行此操作。

要使用 ProfilerModuleViewController 派生类型,请在需要时实例化一个新实例,例如在 ProfilerModule.CreateDetailsViewController 中。

.

using System;
using Unity.Profiling;
using Unity.Profiling.Editor;
using UnityEditor;
using UnityEngine.UIElements;

//--Module--

[Serializable] [ProfilerModuleMetadata("Garbage Collection")] public class GarbageCollectionProfilerModule : ProfilerModule { static readonly ProfilerCounterDescriptor[] k_ChartCounters = new ProfilerCounterDescriptor[] { new ProfilerCounterDescriptor("GC Reserved Memory", ProfilerCategory.Memory), new ProfilerCounterDescriptor("GC Used Memory", ProfilerCategory.Memory), new ProfilerCounterDescriptor("GC Allocated In Frame", ProfilerCategory.Memory), };

// Specify a list of Profiler category names, which should be auto-enabled when the module is active. static readonly string[] k_AutoEnabledCategoryNames = new string[] { ProfilerCategory.Memory.Name, };

public GarbageCollectionProfilerModule() : base(k_ChartCounters, autoEnabledCategoryNames: k_AutoEnabledCategoryNames) { }

public override ProfilerModuleViewController CreateDetailsViewController() { return new GarbageCollectionDetailsViewController(ProfilerWindow); } }

//--Module Custom Drawing--

public class GarbageCollectionDetailsViewController : ProfilerModuleViewController { const string k_UxmlResourceName = "Assets/Editor/GarbageCollectionDetailsView.uxml"; const string k_UxmlElementId_GarbageCollectionDetailsViewBarFill = "garbage-collection-details-viewbar-fill"; const string k_UxmlElementId_GarbageCollectionDetailsViewBarLabel = "garbage-collection-details-viewbar-label";

static readonly ProfilerCounterDescriptor k_GcReservedMemoryCounterDescriptor = new ProfilerCounterDescriptor("GC Reserved Memory", ProfilerCategory.Memory); static readonly ProfilerCounterDescriptor k_GcUsedMemoryCounterDescriptor = new ProfilerCounterDescriptor("GC Used Memory", ProfilerCategory.Memory);

VisualElement m_BarFill; Label m_BarLabel;

public GarbageCollectionDetailsViewController(ProfilerWindow profilerWindow) : base(profilerWindow) { }

protected override VisualElement CreateView() { var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(k_UxmlResourceName); var view = template.Instantiate();

m_BarFill = view.Q<VisualElement>(name: k_UxmlElementId_GarbageCollectionDetailsView__BarFill); m_BarLabel = view.Q<Label>(name: k_UxmlElementId_GarbageCollectionDetailsView__BarLabel);

ReloadData(ProfilerWindow.selectedFrameIndex); ProfilerWindow.SelectedFrameIndexChanged += OnSelectedFrameIndexChanged;

return view; }

protected override void Dispose(bool disposing) { if (!disposing) return;

ProfilerWindow.SelectedFrameIndexChanged -= OnSelectedFrameIndexChanged; base.Dispose(disposing); }

void OnSelectedFrameIndexChanged(long selectedFrameIndex) { ReloadData(selectedFrameIndex); }

void ReloadData(long selectedFrameIndex) { long gcReservedBytes = 0; long gcUsedBytes = 0;

var selectedFrameIndexInt32 = Convert.ToInt32(selectedFrameIndex); using (var frameData = UnityEditorInternal.ProfilerDriver.GetRawFrameDataView(selectedFrameIndexInt32, 0)) { if (frameData == null || !frameData.valid) return;

var gcReservedBytesMarkerId = frameData.GetMarkerId(k_GcReservedMemoryCounterDescriptor.Name); gcReservedBytes = frameData.GetCounterValueAsLong(gcReservedBytesMarkerId);

var gcUsedBytesMarkerId = frameData.GetMarkerId(k_GcUsedMemoryCounterDescriptor.Name); gcUsedBytes = frameData.GetCounterValueAsLong(gcUsedBytesMarkerId); }

float gcUsedBytesScalar = (float)gcUsedBytes / gcReservedBytes; m_BarFill.style.width = new Length(gcUsedBytesScalar * 100, LengthUnit.Percent); m_BarLabel.text = $"{EditorUtility.FormatBytes(gcUsedBytes)} / {EditorUtility.FormatBytes(gcReservedBytes)}"; } }

//--Assets/Editor/GarbageCollectionDetailsView.uxml--

<?xml version="1.0" encoding="utf-8"?> <engine:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:engine="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements" xsi:noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd"> <engine:Style src="GarbageCollectionDetailsView.uss"/> <engine:VisualElement name="garbage-collection-details-view"> <engine:Label name="garbage-collection-details-viewtitle-label" text="Garbage Collection"/> <engine:VisualElement name="garbage-collection-details-viewbar"> <engine:VisualElement name="garbage-collection-details-viewbar-fill"/> </engine:VisualElement> <engine:Label name="garbage-collection-details-viewbar-label"/> </engine:VisualElement> </engine:UXML>

//--Assets/Editor/GarbageCollectionDetailsView.uss--

#garbage-collection-details-view { flex-grow: 1; margin: 8px; }

#garbage-collection-details-view__title-label { -unity-font-style: bold; margin-bottom: 8px; }

#garbage-collection-details-view__bar { background-color: gray; flex-direction: row; height: 20px; }

#garbage-collection-details-view__bar-fill { background-color: #B2194C; position: absolute; height: 100%; }

属性

ProfilerWindow视图控制器所属的 Profiler 窗口。

公共方法

Dispose处理视图控制器。当不再需要视图控制器时,Unity 会自动调用此方法,并且其视图将从窗口层次结构中删除。

受保护的方法

CreateView创建视图控制器的视图。当 Unity 即将首次显示视图控制器的视图时,它会自动调用此方法。