

定制Unity Profiler的性能指标
Unity技术博客
阅读 2210
2022年6月29日
要想优化一个应用,你需要准确地测量出它对硬件的消耗。
Unity Profiler支持制定自定义的性能指标,让你能够更好地了解应用的性能变化。在这篇文章中,我们将介绍
Unity 2021 LTS https://unity.com/releases/2021-lts
新的Profiler扩展功能。新Profiler计量器带有一种轻型的自定义机制,允许你为Unity应用和软件包设定你自己的性能指标。并且,你还能为Profiler添加模块,直接在窗口中可视化这些数据。请在下方了解怎样使用这些功能来提高你项目的性能。
什么是Unity Profiler?
Unity Profiler https://docs.unity3d.com/2021.2/Documentation/Manual/Profiler.html
是一款用于记录应用详细性能的工具。它可以记录大多数类别的性能指标,比如内存、音频和渲染。记录下的指标数据既可在Profiler窗口中查看,有时也能用脚本查询。有了这些信息,你就能深入了解Unity应用如何使用目标平台的硬件资源,找出那些可以优化的地方。
Profiler计量器
什么是Profiler计量器?
Profiler计量器负责跟踪、测量或计算应用产生的性能分析相关指标。比如,Unity本身自带的默认Profiler计量器会跟踪记录应用所使用的总内存字节数,称为“Total Used Memory(总占用内存)”。这项数据在衡量目标设备上应用的内存足迹时非常重要。Profiler窗口的Memory模块会按时间统计该数据以及其他与内存相关的指标,如下图。

自定义Profiler计量器
通过添加自己的Profiler计量器,你就能记录系统或应用内的特定性能指标。这些数据可以在Profiler窗口中与内置计量器等其他性能数据一起展示。而你就能直接在Profiler窗口中配合其他背景数据来查看应用独特的性能特征。
在下例中,一个自定义Profiler计量器会跟踪记录场景内活跃的生物数量。它与内置的Batches Count并排展示,后者记录着系统每帧会处理多少批次的渲染。如此一来,我们可以轻松地关联这两个指标,了解生物数量对批量渲染次数的影响。

左下角为Profiler计量器在游戏UI中的例子
此外,你所有的Profiler计量器包括内置自带的计量器都可以在发行版中使用。发行版比开发版更能代表应用的真实性能。
但是出于优化原因,Unity Profiler并不能添加到发行版的游戏包中。这时,你可以从脚本查询Profiler计量器,有选择地监测发行版的重要指标。你可以用这种方法检测连续整合(Continuous Integration)里可能出现的性能倒退。或者,你也可以用
Unity的UI系统 https://docs.unity3d.com/Manual/UIToolkits.html
在左下角(如下图)等位置显示一块调试界面。更多相关细节详见Profiler Recorder https://docs.unity3d.com/ScriptReference/Unity.Profiling.ProfilerRecorder.html
文档。
在游戏UI的左下角显示的Profiler计量器。
如果你只想在开发版中显示Profiler计量器,可以参照
Conditional Compilation文档 https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
里的方法用DEVELOPMENT_BUILD脚本符号(scripting symbol)将其标出,在发行版中去除Profiler。上手使用
Profiler计量器目前已随
Profiling.Core https://docs.unity3d.com/Packages/com.unity.profiling.core@latest/index.html?subfolder=/manual/profilercounter-guide.html
包发布于Unity 2021 LTS版。Unity 2021 LTS默认下不安装该软件包,请参考文档中的安装说明在Package Manager中查找并安装。在安装完成后,你就能创建Profiler计量器(在API中叫做ProfilerCounter或ProfilerCounterValue)并依照以下方法更新数据。
using UnityEngine; using Unity.Profiling; public class CreatureCounter : MonoBehaviour { static readonly ProfilerCounterValue k_CreatureCounter = new( ProfilerCategory.Scripts, "Creature Count", ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame); void OnEnable() { k_CreatureCounter.Value += 1; } void OnDisable( ) { k_CreatureCounter.Value -= 1; }}
更多相关细节详见
Profiler.Core包文档 https://docs.unity3d.com/Packages/com.unity.profiling.core@latest/index.html?subfolder=/manual/profilercounter-guide.html
。Profiler模块
什么是Profiler的模块?
Profiler窗口内的模块显示着相应领域或流程中的统计信息,如内存、音频或渲染。每个模块都为特定领域的性能概况提供了详细信息。如下方的Memory Profiler模块便展示了内存占用相关的七个关键指标。再下边则是显示某一帧内存分布的详情部分。

Custom Profiler自定义模块
Unity 2021 LTS支持在分析器窗口内定制模块。你可以直接在窗口里显示你自己所选的性能指标。你也可以自定义模块详情界面的可视化方式,让你能以自由地选择性能数据的展示方法。
Profiler Module Editor(模块编辑器)
我们推荐使用Profiler Module Editor来快速创建临时性Profiler模块。比如你可以用临时性Profiler模块来检验某个新Profiler计量器。用Profiler Module Editor创建的模块与本地用户的编辑器绑定,不能在其他项目中使用。

更多详情请参见
模块编辑器文档 https://docs.unity3d.com/Documentation/Manual/profiler-module-editor.html
。Profiler模块的API
Profiler模块API支持将自定义的模块添加到Profiler窗口中,供项目或软件包的所有用户使用。使用该API编写的模块会被自动添加到Profiler窗口,所有项目或软件包的用户都可使用。
资源商店的发布者或软件包开发者可以在包内直接发布自己设置的Profiler模块。用户在安装包的同时也会安装你的Profiler模块。这样一来,你就能直接在Profiler窗口内展示特定的性能指标。
Unity的内部团队已经在使用这个API编写并发布自定义的Profiler模块。目前包含有自定义模块的软件包有
Netcode for GameObjects https://docs-multiplayer.unity3d.com/
、Adaptive Performance https://docs.unity3d.com/Packages/com.unity.adaptiveperformance@latest/index.html
、Mali System Metrics https://docs.unity3d.com/Packages/com.unity.profiling.systemmetrics.mali@latest/index.html
等。使用方法
若想使用该API创建一个Profiler模块,请参照以下代码在项目或包中新建一份ProfilerModule脚本。
using Unity.Profiling; using Unity.Profiling.Editor; [System.Serializable] [ProfilerModuleMetadata("Creature Batches")] public class CreatureBatchesProfilerModule : ProfilerModule { static readonly ProfilerCounterDescriptor[] k_Counters = { new("Creature Count", ProfilerCategory.Scripts), new("Batches Count", ProfilerCategory.Render), }; public CreatureBatchesProfilerModule() : base(k_Counters) { }}
新建的模块将自动出现在Profiler窗口内,供所有用户使用。

关于API的更多介绍请参见Profiler模块的
手册 https://docs.unity3d.com/2022.1/Documentation/Manual/Profiler-creating-custom-modules.html
和API https://docs.unity3d.com/2021.2/Documentation/ScriptReference/Unity.Profiling.Editor.ProfilerModule.html
文档。Profiler模块的API还支持用
UI Toolkit https://docs.unity3d.com/Manual/UIElements.html
等UI系统为某一帧的性能数据绘制自定义的可视化图形。 例如,Adaptive Performance的Profiler模块就使用了该API来展示某一帧的详细性能信息,以及性能瓶颈(Bottleneck)和过热警告(Thermal Warning)等其他背景信息。这些指标可以帮助用户清楚地了解到应用在什么情况下会触发设备的过热保护。所以,你也可以使用该API来自定义Profiler模块的性能数据可视化方法。

关于创建自定义Profiler模块的细节请参考Profiler模块的
手册 https://docs.unity3d.com/2022.1/Documentation/Manual/Profiler-customizing-details-view.html
和API https://docs.unity3d.com/2021.2/Documentation/ScriptReference/Unity.Profiling.Editor.ProfilerModule.CreateDetailsViewController.html
文档。额外的背景数据
有时我们需要用Profiler模块和计量器来统计更多、更复杂的数据。比如在Profiler模块的详情部分展示当前帧的屏幕截图来作参考,如下图。

要想将图片等其他帧数据传给Profiler,并在Profiler记录内展示,你可以使用
Profiler.EmitFrameMetaData https://docs.unity3d.com/2021.2/Documentation/ScriptReference/Profiling.Profiler.EmitFrameMetaData.html
和FrameDataView.GetFrameMetaData https://docs.unity3d.com/2021.2/Documentation/ScriptReference/Profiling.FrameDataView.GetFrameMetaData.html
这两个Frame Metadata API。 如果数据在每次分析时只需记录一次,比如游戏的配置,你也可以使用
Profiler.EmitSessionMetaData https://docs.unity3d.com/2021.2/Documentation/ScriptReference/Profiling.Profiler.EmitSessionMetaData.html
和FrameDataView.GetSessionMetaData https://docs.unity3d.com/2021.2/Documentation/ScriptReference/Profiling.FrameDataView.GetSessionMetaData.html
这两个Session Metadata API。以上功能的用法和适用范围请参考相应链接中的文档。
结论
本文介绍了怎样自定义性能指标、扩展Unity Profiler。我们还了解我们怎样用
Profiling.Core https://docs.unity3d.com/Packages/com.unity.profiling.core@latest/index.html?subfolder=/manual/profilercounter-guide.html
的Profiler计量器来添加自定义指标。然后还探索了如何用Profiler Module Editor和Profiler Module API在窗口中添加自定义模块。最后,我们又解释了怎样向Profiler发送图片等额外数据作背景参考。希望这些扩展功能可以帮助你更好地检测和理解应用的性能。如有任何问题,请随时在
论坛 https://forum.unity.com/forums/profiler-previews.199/
上与我们联系。欢迎大家前来反馈,并帮助我们更好地改进Unity性能工具。发布于技术交流
0条评论

问
AI
全新AI功能上线
1. 基于Unity微调:专为Unity优化,提供精准高效的支持。
2. 深度集成:内置于团结引擎,随时查阅与学习。
3. 多功能支持:全面解决技术问题与学习需求。

问
AI







