Unity runtime性能分析器

一、 Profiler:

1. CPU Usage
A. WaitForTargetFPS:

Vsync(垂直同步)功能所,即显示当前帧的CPU等待时间

B. Overhead:

Profiler总体时间-所有单项的记录时间总和。用于记录尚不明确的时间消耗,以帮助进一步完善Profiler的统计。

C. Physics.Simulate:

当前帧物理模拟的CPU占用时间。

D. Camera.Render:

相机渲染准备工作的CPU占用量

E. RenderTexture.SetActive:

设置RenderTexture操作.

底层实现:1.比对当前帧与前一帧的ColorSurface和DepthSurface.

2.如果这两个Buffer一致则不生成新的RT,否则则生成新的RT,并设置与之相对应的Viewport和空间转换矩阵.

F. Monobehaviour.OnMouse_ :
用于检测鼠标的输入消息接收和反馈,主要包括:SendMouseEvents和DoSendMouseEvents。(只要Edtor开起来,这个就会存在)

G. HandleUtility.SetViewInfo:

仅用于Editor中,作用是将GUI和Editor中的显示看起来与发布版本的显示一致。

H. GUI.Repaint:
GUI的重绘(说明在有使用原生的OnGUI)

I. Event.Internal_MakeMasterEventCurrent:
负责GUI的消息传送

J. Cleanup Unused Cached Data:

清空无用的缓存数据,主要包括RenderBuffer的垃圾回收和TextRendering的垃圾回收。

1.RenderTexture.GarbageCollectTemporary:存在于RenderBuffer的垃圾回收中,清除临时的FreeTexture.

2.TextRendering.Cleanup:TextMesh的垃圾回收操作

K. Application.Integrate Assets in Background:

遍历预加载的线程队列并完成加载,同时,完成纹理的加载、Substance的Update等.

L. Application.LoadLevelAsync Integrate:
加载场景的CPU占用,通常如果此项时间长的话70%的可能是Texture过长导致.

M. UnloadScene:
卸载场景中的GameObjects、Component和GameManager,一般用在切换场景时.

N. CollectGameObjectObjects:

执行上面M项的同时,会将场景中的GameObject和Component聚集到一个Array中.然后执行下面的Destroy.

O. Destroy:
删除GameObject和Component的CPU占用.

P. AssetBundle.LoadAsync Integrate:

多线程加载AwakeQueue中的内容,即多线程执行资源的AwakeFromLoad函数.

Q. Loading.AwakeFromLoad:
在资源被加载后调用,对每种资源进行与其对应用处理.


2. CPU Usage

A. Device.Present:

device.PresentFrame的耗时显示,该选项出现在发布版本中.

B. Graphics.PresentAndSync:

GPU上的显示和垂直同步耗时.该选项出现在发布版本中.

C. Mesh.DrawVBO:

GPU中关于Mesh的Vertex Buffer Object的渲染耗时.

D. Shader.Parse:

资源加入后引擎对Shader的解析过程.

E. Shader.CreateGPUProgram:
根据当前设备支持的图形库来建立GPU工程.

3. Memory Profiler

A. Used Total:

当前帧的Unity内存、Mono内存、GfxDriver内存、Profiler内存的总和.

B. Reserved Total:

系统在当前帧的申请内存.

C. Total System Memory Usage:

当前帧的虚拟内存使用量.(通常是我们当前使用内存的1.5~3倍)

D. GameObjects in Scene:

当前帧场景中的GameObject数量.

E. Total Objects in Scene:

当前帧场景中的Object数量(除GameObject外,还有Component等).

F. Total Object Count:

Object数据 + Asset数量.

4. Detail Memory Profiler

A. Assets:

Texture2d:记录当前帧内存中所使用的纹理资源情况,包括各种GameObject的纹理、天空盒纹理以及场景中所用的Lightmap资源.

B. Scene Memory:

记录当前场景中各个方面的内存占用情况,包括GameObject、所用资源、各种组件以及GameManager等(天般情况通过AssetBundle加载的不会显示在这里).

A. Other:

ManagedHeap.UseSize:代码在运行时造成的堆内存分配,表示上次GC到目前为止所分配的堆内存量.

SerializedFile(3):

WebStream:这个是由WWW来进行加载的内存占用.

System.ExecutableAndDlls:不同平台和不同硬件得到的值会不一样。******************

5. 优化重点

A. CPU-GC Allow:

关注原则:1.检测任何一次性内存分配大于2KB的选项 2.检测每帧都具有20B以上内存分配的选项.

B. Time ms:

记录游戏运行时每帧CPU占用(特别注意占用5ms以上的).

C. Memory Profiler-Other:

1.ManagedHeap.UsedSize: 移动游戏建议不要超过20MB.

2.SerializedFile: 通过异步加载(LoadFromCache、WWW等)的时候留下的序列化文件,可监视是否被卸载.

3.WebStream: 通过异步WWW下载的资源文件在内存中的解压版本,比SerializedFile大几倍或几十倍,重点监视.****

D. Memory Profiler-Assets:

1.Texture2D: 重点检查是否有重复资源和超大Memory是否需要压缩等.

2.AnimationClip: 重点检查是否有重复资源.

3.Mesh: 重点检查是否有重复资源.

6. 项目中可能遇到的问题

A. Device.Present:

1.GPU的presentdevice确实非常耗时,一般出现在使用了非常复杂的shader.

2.GPU运行的非常快,而由于Vsync的原因,使得它需要等待较长的时间.

3.同样是Vsync的原因,但其他线程非常耗时,所以导致该等待时间很长,比如:过量AssetBundle加载时容易出现该问题.

4.Shader.CreateGPUProgram:Shader在runtime阶段(非预加载)会出现卡顿(华为K3V2芯片).

B. StackTraceUtility.PostprocessStacktrace()和StackTraceUtility.ExtractStackTrace():

1.一般是由Debug.Log或类似API造成.

2.游戏发布后需将Debug API进行屏蔽.

C. Overhead:

1.一般情况为Vsync所致.

2.通常出现在Android设备上.

D. GC.Collect:

原因: 1.代码分配内存过量(恶性的) 2.一定时间间隔由系统调用(良性的).

占用时间:1.与现有Garbage size相关 2.与剩余内存使用颗粒相关(比如场景物件过多,利用率低的情况下,GC释放后需要做内存重排)

E. GarbageCollectAssetsProfile:

1.引擎在执行UnloadUnusedAssets操作(该操作是比较耗时的,建议在切场景的时候进行).

2.尽可能地避免使用Unity内建GUI,避免GUI.Repaint过渡GC Allow.

3.if(other.tag == GearParent.MogoPlayerTag)改为other.CompareTag(GearParent.MogoPlayerTag).因为other.tag为产生180B的GC Allow.

F. 少用foreach,因为每次foreach为产生一个enumerator(约16B的内存分配),尽量改为for.

G. Lambda表达式,使用不当会产生内存泄漏.

H. 尽量少用LINQ:

1.部分功能无法在某些平台使用.

2.会分配大量GC Allow.

I. 控制StartCoroutine的次数:

1.开启一个Coroutine(协程),至少分配37B的内存.

2.Coroutine类的实例 -- 21B.

3.Enumerator -- 16B.

J. 使用StringBuilder替代字符串直接连接.

K. 缓存组件:

1.每次GetComponent均会分配一定的GC Allow.

2.每次Object.name都会分配39B的堆内存.

 

二、unity的Log机制

unity4以及之前的版本,使用下面的方式注册回调

Application.RegisterLogCallback(_OnLogCallbackHandler);

unity5以后使用logMessageReceived

Application.logMessageReceived += _OnLogCallbackHandler;

但它接受来自unity自身主线程的log

要想同时可以接受到别的线程的Log,则使用logMessageReceivedThreaded

Application.logMessageReceivedThreaded += LogMultiThreadCallback;

不过在测试logMessageReceivedThreaded时,发现收集到的其他线程的堆栈不太准确。


Log回调不但能接受log信息,还能接收到异常和错误信息,我们就可以利用它来进行崩溃收集

我们过滤log 类型为Error、Assert、Exception的log,即为我们想要的崩溃信息

 

三、C#的异常机制
UnhandledException事件提供未捕获的异常的通知。

System.AppDomain.CurrentDomain.UnhandledException += _OnUnresolvedExceptionHandler;

 

四、FPS简便算法

  void UpDateFPS()
        {
            deltaTime += (Time.deltaTime - deltaTime) * 0.1f;//完成最后一帧的时间秒-上一帧时间时间==这一帧时间秒 /1000=毫秒
            PFpsStr = (1.0f / deltaTime).ToString("f2"); //每帧ms的倒数=频率
        }

}

//FPS官方写法

 private void Update()
        {
            // measure average frames per second
            m_FpsAccumulator++;
            if (Time.realtimeSinceStartup > m_FpsNextPeriod)
            {
                m_CurrentFps = (int) (m_FpsAccumulator/fpsMeasurePeriod);
                m_FpsAccumulator = 0;
                m_FpsNextPeriod += fpsMeasurePeriod;
                m_Text.text = string.Format(display, m_CurrentFps);
            }
        }

 

 

五、Debug:

 

using UnityEngine;

using System.Collections.Generic;

using System;

using UnityEngine.Profiling;

 

public class RuntimeProfiler : MonoBehaviour

{

static RuntimeProfiler _instance;

 

public static RuntimeProfiler instance

{

get

{

if (_instance == null)

{

GameObject go = new GameObject("RuntimeProfilerobj");

DontDestroyOnLoad(go);

_instance = go.AddComponent<RuntimeProfiler>();

}

return _instance;

}

}

 

public bool AllowDebugging = true;

public bool isRunProfiler = false;

 

private DebugType _debugType = DebugType.Console;

private List<LogData> _logInformations = new List<LogData>();

private int _currentLogIndex = -1;

private int _infoLogCount = 0;

private int _warningLogCount = 0;

private int _errorLogCount = 0;

private int _fatalLogCount = 0;

private bool _showInfoLog = true;

private bool _showWarningLog = true;

private bool _showErrorLog = true;

private bool _showFatalLog = true;

private Vector2 _scrollLogView = Vector2.zero;

private Vector2 _scrollCurrentLogView = Vector2.zero;

private Vector2 _scrollSystemView = Vector2.zero;

private bool _expansion = false;

private Rect _windowRect = new Rect(150, 100, 130, 90);

private int _fps = 0;

private Color _fpsColor = Color.white;

private int _frameNumber = 0;

private float _lastShowFPSTime = 0f;

 

void Awake()

{

_instance = this;

 

}

 

void Start()

{

if (AllowDebugging)

{

Application.logMessageReceived += LogHandler;

}

}

 

private void Update()

{

if (AllowDebugging)

{

_frameNumber += 1;

float time = Time.realtimeSinceStartup - _lastShowFPSTime;

if (time >= 1)

{

_fps = (int)(_frameNumber / time);

_frameNumber = 0;

_lastShowFPSTime = Time.realtimeSinceStartup;

}

}

}


 

/// <summary>

///自动销毁

/// </summary>

public void Reset()

{

//TODO

Destroy(gameObject);

}

 

private void OnDestory()

{

if (AllowDebugging)

{

Application.logMessageReceived -= LogHandler;

}

_instance = null;

}

 

private void LogHandler(string condition, string stackTrace, LogType type)

{

LogData log = new LogData();

log.time = DateTime.Now.ToString("HH:mm:ss");

log.message = condition;

log.stackTrace = stackTrace;

 

if (type == LogType.Assert)

{

log.type = "Fatal";

_fatalLogCount += 1;

}

else if (type == LogType.Exception || type == LogType.Error)

{

log.type = "Error";

_errorLogCount += 1;

}

else if (type == LogType.Warning)

{

log.type = "Warning";

_warningLogCount += 1;

}

else if (type == LogType.Log)

{

log.type = "Info";

_infoLogCount += 1;

}

 

_logInformations.Add(log);

 

if (_warningLogCount > 0)

{

_fpsColor = Color.yellow;

}

if (_errorLogCount > 0)

{

_fpsColor = Color.red;

}

}

 

private void OnGUI()

{

if (AllowDebugging && isRunProfiler)

{

if (_expansion)

{

_windowRect = GUI.Window(0, _windowRect, ExpansionGUIWindow, "RuntimeProfiler");

}

else

{

_windowRect = GUI.Window(0, _windowRect, ShrinkGUIWindow, "DEBUGGER");

}

}

}

 

private void ExpansionGUIWindow(int windowId)

{

GUI.DragWindow(new Rect(0, 0, 2000, 40));

 

#region title

GUILayout.BeginHorizontal();

GUI.contentColor = _fpsColor;

if (GUILayout.Button("FPS:" + _fps, GUILayout.Height(50)))

{

 

_expansion = false;

_windowRect.width = 130;

_windowRect.height = 90;

// Debug.Log("GUILayout.Button FPS" + _expansion);

}

GUI.contentColor = (_debugType == DebugType.Console ? Color.white : Color.gray);

if (GUILayout.Button("Console", GUILayout.Height(50)))

{

_debugType = DebugType.Console;

}

GUI.contentColor = (_debugType == DebugType.Memory ? Color.white : Color.gray);

if (GUILayout.Button("Memory", GUILayout.Height(50)))

{

_debugType = DebugType.Memory;

}

GUI.contentColor = (_debugType == DebugType.System ? Color.white : Color.gray);

if (GUILayout.Button("System", GUILayout.Height(50)))

{

_debugType = DebugType.System;

}

GUI.contentColor = (_debugType == DebugType.Screen ? Color.white : Color.gray);

if (GUILayout.Button("Screen", GUILayout.Height(50)))

{

_debugType = DebugType.Screen;

}

GUI.contentColor = (_debugType == DebugType.Quality ? Color.white : Color.gray);

if (GUILayout.Button("Quality", GUILayout.Height(50)))

{

_debugType = DebugType.Quality;

}

GUI.contentColor = (_debugType == DebugType.Environment ? Color.white : Color.gray);

if (GUILayout.Button("Environment", GUILayout.Height(50)))

{

_debugType = DebugType.Environment;

}

GUI.contentColor = Color.white;

GUILayout.EndHorizontal();

#endregion

 

#region console

if (_debugType == DebugType.Console)

{

GUILayout.BeginHorizontal();

if (GUILayout.Button("Clear"))

{

_logInformations.Clear();

_fatalLogCount = 0;

_warningLogCount = 0;

_errorLogCount = 0;

_infoLogCount = 0;

_currentLogIndex = -1;

_fpsColor = Color.white;

}

GUI.contentColor = (_showInfoLog ? Color.white : Color.gray);

_showInfoLog = GUILayout.Toggle(_showInfoLog, "Info [" + _infoLogCount + "]");

GUI.contentColor = (_showWarningLog ? Color.white : Color.gray);

_showWarningLog = GUILayout.Toggle(_showWarningLog, "Warning [" + _warningLogCount + "]");

GUI.contentColor = (_showErrorLog ? Color.white : Color.gray);

_showErrorLog = GUILayout.Toggle(_showErrorLog, "Error [" + _errorLogCount + "]");

GUI.contentColor = (_showFatalLog ? Color.white : Color.gray);

_showFatalLog = GUILayout.Toggle(_showFatalLog, "Fatal [" + _fatalLogCount + "]");

GUI.contentColor = Color.white;

GUILayout.EndHorizontal();

 

_scrollLogView = GUILayout.BeginScrollView(_scrollLogView, "Box", GUILayout.Height(480));

for (int i = 0; i < _logInformations.Count; i++)

{

bool show = false;

Color color = Color.white;

switch (_logInformations[i].type)

{

case "Fatal":

show = _showFatalLog;

color = Color.red;

break;

case "Error":

show = _showErrorLog;

color = Color.red;

break;

case "Info":

show = _showInfoLog;

color = Color.white;

break;

case "Warning":

show = _showWarningLog;

color = Color.yellow;

break;

default:

break;

}

 

if (show)

{

GUILayout.BeginHorizontal();

if (GUILayout.Toggle(_currentLogIndex == i, ""))

{

_currentLogIndex = i;

}

GUI.contentColor = color;

GUILayout.Label("[" + _logInformations[i].type + "] ");

GUILayout.Label("[" + _logInformations[i].time + "] ");

GUILayout.Label(_logInformations[i].message);

GUILayout.FlexibleSpace();

GUI.contentColor = Color.white;

GUILayout.EndHorizontal();

}

}

GUILayout.EndScrollView();

 

_scrollCurrentLogView = GUILayout.BeginScrollView(_scrollCurrentLogView, "Box", GUILayout.Height(320));

if (_currentLogIndex != -1)

{

GUILayout.Label(_logInformations[_currentLogIndex].message + "\r\n\r\n" + _logInformations[_currentLogIndex].stackTrace);

}

GUILayout.EndScrollView();

}

#endregion

 

#region memory

else if (_debugType == DebugType.Memory)

{

GUILayout.BeginHorizontal();

GUILayout.Label("Memory Information");

GUILayout.EndHorizontal();

 

GUILayout.BeginVertical("Box");

#if UNITY_5

 

GUILayout.Label("总内存:" + Profiler.GetTotalReservedMemoryLong() / 1000000 + "MB");

GUILayout.Label("已占用内存:" + Profiler.GetTotalAllocatedMemoryLong() / 1000000 + "MB");

GUILayout.Label("空闲中内存:" + Profiler.GetTotalUnusedReservedMemoryLong() / 1000000 + "MB");

GUILayout.Label("总Mono堆内存:" + Profiler.GetMonoHeapSizeLong() / 1000000 + "MB");

GUILayout.Label("已占用Mono堆内存:" + Profiler.GetMonoUsedSizeLong() / 1000000 + "MB");

#endif

GUILayout.EndVertical();

 

GUILayout.BeginHorizontal();

if (GUILayout.Button("卸载未使用的资源"))

{

Resources.UnloadUnusedAssets();

}

GUILayout.EndHorizontal();

 

GUILayout.BeginHorizontal();

if (GUILayout.Button("使用GC垃圾回收"))

{

GC.Collect();

}

GUILayout.EndHorizontal();

}

#endregion

 

#region system

else if (_debugType == DebugType.System)

{

GUILayout.BeginHorizontal();

GUILayout.Label("System Information");

GUILayout.EndHorizontal();

 

_scrollSystemView = GUILayout.BeginScrollView(_scrollSystemView, "Box");

GUILayout.Label("操作系统:" + SystemInfo.operatingSystem);

GUILayout.Label("系统内存:" + SystemInfo.systemMemorySize + "MB");

GUILayout.Label("处理器:" + SystemInfo.processorType);

GUILayout.Label("处理器数量:" + SystemInfo.processorCount);

GUILayout.Label("显卡:" + SystemInfo.graphicsDeviceName);

GUILayout.Label("显卡类型:" + SystemInfo.graphicsDeviceType);

GUILayout.Label("显存:" + SystemInfo.graphicsMemorySize + "MB");

GUILayout.Label("显卡标识:" + SystemInfo.graphicsDeviceID);

GUILayout.Label("显卡供应商:" + SystemInfo.graphicsDeviceVendor);

GUILayout.Label("显卡供应商标识码:" + SystemInfo.graphicsDeviceVendorID);

GUILayout.Label("设备模式:" + SystemInfo.deviceModel);

GUILayout.Label("设备名称:" + SystemInfo.deviceName);

GUILayout.Label("设备类型:" + SystemInfo.deviceType);

GUILayout.Label("设备标识:" + SystemInfo.deviceUniqueIdentifier);

GUILayout.EndScrollView();

}

#endregion

 

#region screen

else if (_debugType == DebugType.Screen)

{

GUILayout.BeginHorizontal();

GUILayout.Label("Screen Information");

GUILayout.EndHorizontal();

 

GUILayout.BeginVertical("Box");

GUILayout.Label("DPI:" + Screen.dpi);

GUILayout.Label("分辨率:" + Screen.currentResolution.ToString());

GUILayout.EndVertical();

 

GUILayout.BeginHorizontal();

if (GUILayout.Button("全屏"))

{

Screen.SetResolution(Screen.currentResolution.width, Screen.currentResolution.height, !Screen.fullScreen);

}

GUILayout.EndHorizontal();

}

#endregion

 

#region Quality

else if (_debugType == DebugType.Quality)

{

GUILayout.BeginHorizontal();

GUILayout.Label("Quality Information");

GUILayout.EndHorizontal();

 

GUILayout.BeginVertical("Box");

string value = "";

if (QualitySettings.GetQualityLevel() == 0)

{

value = " [最低]";

}

else if (QualitySettings.GetQualityLevel() == QualitySettings.names.Length - 1)

{

value = " [最高]";

}

 

GUILayout.Label("图形质量:" + QualitySettings.names[QualitySettings.GetQualityLevel()] + value);

GUILayout.EndVertical();

 

GUILayout.BeginHorizontal();

if (GUILayout.Button("降低一级图形质量"))

{

QualitySettings.DecreaseLevel();

}

GUILayout.EndHorizontal();

 

GUILayout.BeginHorizontal();

if (GUILayout.Button("提升一级图形质量"))

{

QualitySettings.IncreaseLevel();

}

GUILayout.EndHorizontal();

}

#endregion

 

#region Environment

else if (_debugType == DebugType.Environment)

{

GUILayout.BeginHorizontal();

GUILayout.Label("Environment Information");

GUILayout.EndHorizontal();

 

GUILayout.BeginVertical("Box");

GUILayout.Label("项目名称:" + Application.productName);

#if UNITY_5

GUILayout.Label("项目ID:" + Application.identifier);

#endif

#if UNITY_7

GUILayout.Label("项目ID:" + Application.identifier);

#endif

GUILayout.Label("项目版本:" + Application.version);

GUILayout.Label("Unity版本:" + Application.unityVersion);

GUILayout.Label("companyName:" + Application.companyName);

GUILayout.EndVertical();

 

GUILayout.BeginHorizontal();

if (GUILayout.Button("退出程序"))

{

Application.Quit();

}

GUILayout.EndHorizontal();

}

#endregion

}

 

private void ShrinkGUIWindow(int windowId)

{

GUI.DragWindow(new Rect(0, 0, 2000, 40));

 

GUI.contentColor = _fpsColor;

if (GUILayout.Button("FPS:" + _fps, GUILayout.Width(100), GUILayout.Height(50)))

{

_expansion = true;

_windowRect.width = 1700;

_windowRect.height = 800;

// Debug.Log("GUILayout.Button FPS2" + _expansion);

}

GUI.contentColor = Color.white;

}

 

}

 

public struct LogData

{

public string time;

public string type;

public string message;

public string stackTrace;

}

 

public enum DebugType

{

Console,

Memory,

System,

Screen,

Quality,

Environment

}

 

转载于:https://www.cnblogs.com/cxihu/p/10273881.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值