可视性能分析器数器
将性Add Profiler到自定义性能性能分析器模块。
阅读时间7 分钟最后更新于 6 天前
向代码添加性能分析器计数器后,即可在性能分析器窗口或应用程序的版本中可视化它们。
在 性能分析器 窗口中可视化自定义计数器
要查看或在性能分析器窗口中生成的数据,可以使用自定义性能分析器模块。这可能有助于直观识别与OtherSystemMetrics的关系并快速识别性能问题。
ProfilerCounterProfilerCounterValue向代码添加 Metrics
以下示例假设 GameManager 类处理高关卡逻辑并了解敌人。要更新计数器的值,请使用 或 方法(取决于何时对生成或销毁敌人执行逻辑 ON),使用 方法将敌人的数量值推送到性能分析器:
UpdateLateUpdateSampleusing UnityEngine;using Unity.Profiling;类 GameManager:MonoBehaviour{ Enemy[] m_Enemies; void Update() { GameStats.EnemyCount.Sample(m_Enemies.Length); }}
要将子弹数量通道给性能性能分析器,此示例假设有一个 shell 脚本可以管理子弹Lifecycle。然后以为单位增加值并减小该值,便提供有关游戏中当前子弹流的准确信息。
AwakeGameStats.BulletCountOnDestroyusing UnityEngine;using Unity.Profiling;public class Shell :MonoBehaviour{ void Awake() { GameStats.BulletCount.Value += 1; } void OnDestroy() { GameStats.BulletCount.Value -= 1; }}
将自定义定义模块添加到 性能分析器 窗口
使用性能分析器模块 Editor 为可视化选择内置或新添加的计数器。要Open性能分析器模块Editor器:
- 打开性能分析器 Window (Window > Analysis > Performance Analytics)。
- 选择窗口左上角的性能分析器模块 Dropdown。单击齿轮图标,性能分析器模块 Editor 将在新建窗口中打开。
- 选择 Add,然后从窗口右侧 ON 的用户列表中选择要可视化的计数器。
然后,您可以在 性能分析器 窗口中与Other计数器一起View数据。有关更多信息,请参阅创建自定义性能分析器模块。

Module with custom counters in Profiler Window.
在构建的播放Player中可视化性能性能分析器数器
在构建的播放Player中运行Projectject,无法访问 性能分析器 窗口。但是,您可以在构建的 Player 中将计数器显示为 UI 元素。这意味着您可以在应用程序的版本中包含分析工具。
下图使用内置 Player 中的自定义 UI 在 Scene 的左上角显示计数器:

Tanks game with custom information overlaid
获取玩家中的计数器值
性能分析器 计数器可让您深入了解重要的游戏或引擎SystemMetrics。如果您有 Continuous 集成设置,或者希望在测试播放期间可视化应用程序中的 Key Performance Metrics,可以使用 API 获取自定义和内置性能分析器计数器值。
ProfilerRecorder例如,以下脚本显示帧时间、Mono/IL2CPP 堆大小以及应用程序使用的总计内存。
using System.Collections.Generic;using System.Text;using Unity.Profiling;using UnityEngine;public class StatsScript :MonoBehaviour{ string statsText; ProfilerRecorder systemMemoryRecorder; ProfilerRecorder gcMemoryRecorder; ProfilerRecorder mainThreadTimeRecorder; double GetRecorderFrameAverage(ProfilerRecorder 录制器) { var samplesCount = recorder.Capacity; if (samplesCount == 0) return 0; double r = 0; 不安全 { var samples = stackalloc ProfilerRecorderSample[samplesCount]; recorder.CopyTo(samples, samplesCount); for (var i = 0; i < samplesCount; ++i) r += 样本]值; r /= samplesCount; } return r; } void OnEnable() { systemMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "System Used Memory"); gcMemoryRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Memory, "GC Reserved Memory"); mainThreadTimeRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Internal, "Main Thread", 15); } void OnDisable() { systemMemoryRecorder.Dispose(); gcMemoryRecorder.Dispose(); mainThreadTimeRecorder.Dispose(); } void Update() { var sb = new StringBuilder(500); sb.AppendLine($"Frame Time: {GetRecorderFrameAverage(mainThreadTimeRecorder) * (1e-6f):F1} ms"); sb.AppendLine($"GC Memory: {gcMemoryRecorder.LastValue / (1024 * 1024)} MB"); sb.AppendLine($"System Memory: {systemMemoryRecorder.LastValue / (1024 * 1024)} MB"); statsText = sb.ToString(); } void OnGUI() { GUI.TextArea(new Rect(10, 30, 250, 50), statsText); }}
从 Editor 中的性能分析器帧数据获取计数器值
要在 Editor 中处理性能分析器帧数据时获取性能分析器计数器值,请使用 API。
FrameDataView您可以使用 、、 和 来获取指定计数器的帧值,如下所示:
FrameDataView.GetCounterValueAsIntFrameDataView.GetCounterValueAsLongFrameDataView.GetCounterValueAsFloatFrameDataView.GetCounterValueAsDoubleusing UnityEditor.Profiling;类 示例{ Static 整型 ExtractMyCounterValue(FrameDataView frameData, 字符串 counterName) { var counterMarkerId = frameData.GetMarkerId(counterName); return frameData.GetCounterValueAsInt(counterMarkerId); }}