unity 之Profiler overview

开始工作:

Window > Analysis > Profiler 或者 Ctrl+7 快捷键

Profiler记录最近300帧的数据,现实每一帧的信息,你可以在这里看到拖慢你应用运行速度的资源和代码

This page covers the following topics:

Profiler window layout

The Profiler window is laid out as follows:

  • A: Profiler controls.使用这些控件来设置从哪个设备进行分析,以及Unity应该执行哪种分析,在帧之间导航,
  • B: Context menu.:色盲模式。之后红和绿
  • C: Profiler modules. 需要分析的模块,你可以自己添加
  • D: Frame charts. 帧表格,里面是每帧的信息,给个个颜色或者曲线代表不同的信息,方块可以打勾,点击之后变成黑色,表示右边不显示该条数据的分析
  • E: Module details panel. 这个区域的信息根据你上面选择的profiler而改变.比如当你选择Cpu模块时,它包含timeline,当你选择 Rendering Profiler 模块时,会显示 debugging信息.

Profiler controls

 这些按钮开始或者听着记录分析数据

ControlFunction
Record打开开始记录分析你选中模块的数据。如果没有启用此按钮,则在运行应用程序时不会收集任何数据。
Deep Profile启用之后,分析所有c#的方法,当你打开后,unity会增加所有mono脚本的介绍,这样就可以对您的脚本进行更详细的调查了
. See Deep Profiling.
Profile Editor分析编辑器,现实editor下正在使用的资源
Attach to Player选择要对应用程序进行概要分析的目标。默认是Editor.Unity还可以自动检测任何运行在你的网络上或通过USB连接的设备, 在下拉列表中显示它们.在下拉菜单中单击Enter IP,手动输入要对应用程序进行概要分析的设备的IP地址. For more information, see Profiling your application.
Allocation Callstacks记录用于脚本内存分配的调用堆栈, 下拉列表选择 Managed Allocations. 会显示GC信息.
Clear on Play在开始时清除信息
Clear清除所有的信息
Load加载已经存储的 Profiler data ,你也可以通过 Profiler.logFile 来加载二进制数据
按住shift键,点击Load按钮,能够将已保存的文件追加到当前帧得解析文件里面
Save保存一个.data文件
Transport controlsUse these controls to navigate around the Profiler charts. If you are profiling in Play mode and you click on any of these transport controls while your application is running, your application pauses.
 Frame numberIndicates the frame number you are currently looking at in the Profiler.
 Back arrowNavigate one frame back.
 Forward arrowNavigate one frame forward.
 CurrentJump to the last recorded frame and make the Profiler display data collected in real time.

Deep Profiling

通常 Profiler只分析在ProfilerMarkers 中标记的代码的时间,这包括从引擎本地代码到脚本代码的第一次的调用堆栈,比如 MonoBehaviour’s Start, Update,等生命周期函数

如果没有在代码中添加ProfilerMarker 标签,unity之分分析unity API中的函数,大多数APO调用都会有一个标头,比如 Camera.main 会被注册 “FindMainCamera”的标签

当你打开 Deep Profile 时,Profiler会分析记录你脚本中的所有方法调用,包括unity自身的api,开启Deep Profiling 会非常耗资源,所有一般大型游戏,不建议开启,小型游戏可以开启,你也可以调节 Profiler.maxUsedMemory 属性增大分析运行内存

 Deep Profiling 分析的数据会在 CPU Usage Profiler 模块中显示

如果你想在不开启Deep Profiling情况下找到哪个调用导致了GC回收,你可以打开 Allocation Callstacks.然后可以在timeline中选择 GC.Alloc samples或者使用  Show Related Objects panel 在视图中查找调用堆栈

ProfilerMarker

Description

用于分析任意代码块的性能标记。

使用 ProfilerMarker 去在代码中标记要在Profiler中分析的代码,分析信息会展示在CPU Profiler

using Unity.Profiling;

public class MySystemClass
{
    static ProfilerMarker s_PreparePerfMarker = new ProfilerMarker("MySystem.Prepare");
    static ProfilerMarker s_SimulatePerfMarker = new ProfilerMarker("MySystem.Simulate");

    public void UpdateLogic()
    {
        s_PreparePerfMarker.Begin();
        // ...
        s_PreparePerfMarker.End();

        using (s_SimulatePerfMarker.Auto())
        {
            // ...
        }
    }
}

ProfilerMarker代表分析代码块的标记,Profiler.BeginSample 传送整个字符串到数据流,ProfilerMarker.Begin and CustomSampler.Begin只传送整型

Constructors

ProfilerMarker创建一个ProfilerMarker用来标记代码用于分析

Public Methods

Auto使用using 代码结构,自动开始和结束
Begin开始分析
End结束分析

Color Blind Mode

色盲模式:开始之后,颜色对比度增大

 

Profiler modules

数据分析的模块:

 

 CPU Usage 模块为你提供应用程序每一帧耗费多长时间,其它模块为你提供其他的信息

当你选择其中一个模块时,更多的细节会展示在底部的窗口

Profiler moduleFunction
CPU Usage显示应用程序在物理、脚本、动画和垃圾收集等领域花费的时间。.
GPU Usage显示与图形处理相关的信息。默认情况下,这个模块不开启的,因为它有很高的开销
Rendering显示Unity在应用程序中如何渲染图形的信息, 包括 static and dynamic batching
, SetPass and Draw calls, triangles, and vertices.
Memory显示Unity如何在应用程序中分配内存的信息.这对于查看脚本分配(GC.Alloc)如何导致垃圾收集特别有用,或应用程序的资产内存使用随时间的变化趋势
AudioDisplays information related to the audio in your application,比如有多少audio sources
在播放, Audio system需要多少 CPU内存
Video显示应用程序中与视频相关的信息。
Physics显示物理引擎处理过的应用程序中的物理信息
Physics (2D)与物理分析器模块类似,该模块显示物理引擎在应用程序中处理2D物理的信息。
Network Messages(deprecated)不顶用了现在,被抛弃了
Network Operations (deprecated)被抛弃了
UI显示关于Unity如何处理您的应用程序的UI批处理的信息,包括为什么以及如何批处理项目. See UI Profiler module.
UI Details与UI模块类似,此模块的图表添加了有关批batch处理和vertices count顶点计数的数据,以及包含点击UI更改的input events的标记。
Global Illumination显示关于Unity在应用程序中的全局照明子系统上花费了多少CPU资源的信息

Profiler module overhead

一些模块分析起来性能耗费比较大,你可以关掉这个窗口,不分析它,但是对 CPU Usage不管用,因为其他模块依赖它来分析

如果你通过Profiler.logFile去收集信息,而不是通过  Profiler窗口,你可以通过 Profiler.SetAreaEnabled().关闭模块

Profiler frame charts

Profiler window展示最近 300 帧的分析数据

Command line arguments

If you start your built Player or the Unity Editor from the command line (such as the Command Prompt on Windows, Terminal on macOS, Linux shell, or adb for Android), you can pass command line arguments to configure some Profiler settings.

Command line argumentDescription
-deepprofilingEnable Deep Profiling in a built Player. This only works with Players built against the Mono Scripting Backend
.
-profileStartupProfile the start-up of a Player or the Editor. When you use this argument with a Player, it has the same effect as building the Player with the Autoconnect Profiler option enabled in Build Settings.

When you use this argument with the Editor, it starts collecting and displaying Profiler information in the Profiler window on start-up of the Editor.
-profiler-maxusedmemory默认, 运行内存为maxUsedMemory is 16MB 编辑内存为256MB for the Editor.你也可以通过maxUsedMemory 去设置 (for example, -profiler-maxusedmemory 16777216). 他的大小默认是以字节为单位
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TO_ZRG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值