下文有两个方法,分别是一段检测执行过程耗费时间的代码,还有一个是保存和加载Unity Profiler 的代码(因为UnityProfiler 只能显示一部分的数据,如果运行时间长的话有部分分析数据查看不到)

  

  1. 检测执行耗费时间 代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine; public class CustomTimer : IDisposable
{
private string m_timerName;
private int m_numTests;
private Stopwatch m_watch; public CustomTimer(string timerName,int numTests)
{
m_timerName = timerName;
m_numTests = numTests; if (m_numTests <= 0)
m_numTests = 1;
m_watch = Stopwatch.StartNew();
} public void Dispose()
{
m_watch.Stop();
float ms = m_watch.ElapsedMilliseconds;
UnityEngine.Debug.Log(string.Format("{0} finished: {1:0.00}ms total , {2:0.00000}ms per test for {3} tests", m_timerName, ms, ms / m_numTests, m_numTests));
}
}

使用方法如下: 在程序块里放入自己要检测的代码,在循环使用的时候,把0改成循环的次数就可以了

using (new CustomTimer("My Test", 0))
{
using (UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url))
{
yield return request.SendWebRequest();
if (request.isHttpError || request.isNetworkError)
{
// 下载出错
print(request.error);
}
else
{ // 下载完成
AssetBundle assetBundle = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
GameObject obj = assetBundle.LoadAsset<GameObject>(assetBundle.GetAllAssetNames()[0]); GameObject obj2 = GameObject.Instantiate(obj);
this.obj = obj2.transform;
// 优先释放request 会降低内存峰值
request.Dispose();
}
}
}

2. Unity Profiler 保存方法:

  

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Profiling; public class ProfilerDataSaverComponent : MonoBehaviour
{
int _count = 0;
void Start()
{
Profiler.logFile = "";
} // Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.LeftControl)&& Input.GetKeyDown(KeyCode.H))
{
StartCoroutine(SaveProfilerData());
}
} IEnumerator SaveProfilerData()
{
while(true)
{
string filepath = Application.persistentDataPath + "/profilerLog" + _count; Profiler.logFile = filepath;
Profiler.enableBinaryLog = true;
Profiler.enabled = true; for (int i = 0; i < 300; i++)
{
yield return new WaitForEndOfFrame();
if(!Profiler.enabled)
{
Profiler.enabled = true; } }
_count++;
}
}
}

  读取保存的UnityProfiler数据: 在Window/ProfilerDataLoader 内

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using UnityEngine.Profiling; public class ProfilerDataLoaderWindow : EditorWindow
{
static List<string> s_cachedFilePaths;
static int s_chosenIndex = -1;
[MenuItem("Window/ProfilerDataLoader")]
static void Init()
{
ProfilerDataLoaderWindow window = (ProfilerDataLoaderWindow)EditorWindow.GetWindow(typeof(ProfilerDataLoaderWindow));
} static void ReadProfilerDataFiles()
{ Profiler.logFile = "";
string[] filePath = Directory.GetFiles(Application.persistentDataPath , "ProfilerLog*"); s_cachedFilePaths = new List<string>(); Regex test = new Regex(".data$"); for (int i = 0; i < filePath.Length; i++)
{
string thisPath = filePath[i]; Match match = test.Match(thisPath); if(!match.Success)
{
Debug.Log("Found file: " + thisPath); s_cachedFilePaths.Add(thisPath);
}
}
s_chosenIndex = -1;
} private void OnGUI()
{
if(GUILayout.Button("Find Files"))
{
ReadProfilerDataFiles();
} if (s_cachedFilePaths == null)
return; EditorGUILayout.Space();
EditorGUILayout.LabelField("Files");
EditorGUILayout.BeginHorizontal(); GUIStyle defaultStyle = new GUIStyle(GUI.skin.button);
defaultStyle.fixedWidth = 40; GUIStyle highlightedStyle = new GUIStyle(defaultStyle);
highlightedStyle.normal.textColor = Color.red; for (int i = 0; i < s_cachedFilePaths.Count; i++)
{
if(i%5==0)
{
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
} GUIStyle thisStyle = null; if(s_chosenIndex ==i)
{
thisStyle = highlightedStyle; }
else
{
thisStyle = defaultStyle;
} if (GUILayout.Button("" + i, thisStyle))
{
Profiler.AddFramesFromFile(s_cachedFilePaths[i]);
s_chosenIndex = i;
}
} EditorGUILayout.EndHorizontal();
}
}

  

Unity 性能分析小工具的更多相关文章

  1. Linux性能分析命令工具汇总

    转自:http://rdc.hundsun.com/portal/article/731.html?ref=myread 出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望,因此整理了这篇文章. ...

  2. JavaScript 性能分析新工具 OneProfile

    OneProfile 是一个网页版的小工具,可以用全新的方式展示 JavaScript 性能分析的结果,帮助开发者洞悉函数调用关系,优化应用性能. 点击打开 OneProfile 背景 Chrome ...

  3. Android性能分析Systrace工具

    一.概述 保证系统流畅度,也就是保证系统能连续不间断地提供每秒60帧的运行状态.当出现掉帧时(也可称为Jank),需要知道当前整个系统所处的状态,systrace便是最佳的选择,它能手机检测Andro ...

  4. 性能分析之工具使用——cpu、io 、mem【工具分析】

    nmon nmon 是一种在aix 与各种 Linux 操作系统上广泛使 用的监控与与分析工具,他主要记录以下内容: • cpu 占用率 • 内存使用情况 • 磁盘I/O 速度.传输和读写比率 • 文 ...

  5. 后台故障&性能分析常用工具

    说明 本文是一个归纳总结,把常用的一些指令,及它们常用的option简单记录了一下,目的是当我们需要工具去定位问题的时候,能够从中找到合适的工具,具体的用法网上有很多博文了,当然还有man手册.参考了 ...

  6. Linux Performance Analysis and Tools(Linux性能分析和工具)

    首先来看一张图: 上面这张神一样的图出自国外一个Lead Performance Engineer(Brendan Gregg)的一次分享,几乎涵盖了一个系统的方方面面,任何人,如果没有完善的计算系统 ...

  7. 【Unity]】AR小工具-Vuforia

    很有意思的增强现实玩具,六分钟应用. https://www.youtube.com/watch?v=khavGQ7Dy3c

  8. Traceview 性能分析工具

    简介 TraceView 是 Android 平台配备一个很好的性能分析的工具.它可以通过图形化的方式让我们了解我们要跟踪的程序的性能,并且能具体到 method.详细内容参考:http://deve ...

  9. Linux 性能分析工具汇总合集

    出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望,因此整理了这篇文章.本文也可以作为检验基础知识的指标,另外文章涵盖了一个系统的方方面面.如果没有完善的计算机系统知识,网络知识和操作系统知识, ...

  10. [转]Linux性能分析工具汇总合集

    出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望,因此整理了这篇文章.本文也可以作为检验基础知识的指标,另外文章涵盖了一个系统的方方面面.如果没有完善的计算机系统知识,网络知识和操作系统知识, ...

随机推荐

  1. 基于Udp通讯的Java局域网群聊小程序

    /**基于Udp通讯的Java局域网群聊小程序 */package com.UdpDemo; import java.net.*; import java.awt.*; import java.awt ...

  2. pycharm下载 安装使用

    pycharm下载与使用 1.下载 ​ 该软件分免费版和收费版 ​ 免费版(community):功能少 ​ 收费版(professional):30天试用 ​ 我们尽量使用收费版本 ​ 官网地址:h ...

  3. avalonia自定义弹窗

    对于使用avalonia的时候某些功能需要到一些提示,比如异常或者成功都需要对用户进行提示,所以需要单独实现弹窗功能,并且可以自定义内部组件,这一期将手动实现一个简单的小弹窗,并且很容易自定义 创建项 ...

  4. 一次生产环境CPU占用高的排查

    1. 项目背景 甲方是保密级别非常高的政府部门.所以我们全程拿不到任何测试数据,只能是自己模拟数据进行测试. 项目部署的时候,公司派了一人到甲方现场,在甲方客户全程监督下,进行部署,调试,导入数据等工 ...

  5. vscode环境配置(C/C++)

    一.MinGW和vscode的简单了解 1.MinGW是什么? MinGW(Minimalist GNU on Windows).它实际上是将经典的开源 C语言 编译器 GCC 移植到了 Window ...

  6. 《价值流动-Project To Product》读后感

    背景:2022年8月,在这家公司已经任职满两年.这两年我最大的成就就是基于Scrum模式推行了敏捷项目管理,并取得了一定的成果.但是,在推行了两年后新的问题也产生了.例如: 1,项目管理上,项目经理缺 ...

  7. vue的异步组件

    异步组件 异步组件:可以在首页加载之前先加载的组件,主要是做性能优化,提高用户体验 一.基本用法 在大型项目中,我们可能需要拆分应用为更小的块,并仅在需要时再从服务器加载相关组件.Vue 提供了 de ...

  8. 列表(list)内置方法补充、字典(dict)内置方法、元组(tuple)内置方法、集合(set)内置方法

    目录 一.列表(list)内置方法补充 二.字典(dict)内置方法 三.元组(tuple)内置方法 四.集合(set)内置方法 一.列表(list)内置方法补充 # reverse()颠倒列表内元素 ...

  9. async异步编程屏蔽凡人

    为提高效率与增加逼格,墙裂建议引入async异步编程,这不比基于threading的线程.基于multiprocessing的进程香?这些大家伙太重量级了,代码又啰里啰唆的一点也不pythonic,只 ...

  10. JZOJ 3737. 【NOI2014模拟7.11】挖宝藏

    \(\text{Solution}\) 当 \(h=1\) 时显然是斯坦纳树板子,最方案必然是树形的 \(h > 1\) 时,考虑在每一层新建一个状态表示上一层宝藏全部挖完到这层某个点的答案 同 ...