Skip to content

Instantly share code, notes, and snippets.

@kirillrybin
Created February 8, 2016 08:53
Show Gist options
  • Save kirillrybin/5ac446c190089e28179e to your computer and use it in GitHub Desktop.
Save kirillrybin/5ac446c190089e28179e to your computer and use it in GitHub Desktop.
ProfilerToFile via http://pastebin.com/ktVgrxyq
using System;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
// Add this component to any go in scene. It will save profiling to a folder next to .exe
//
// in editor use right click on the component name to navigate through files
class ProfilerToFile : MonoBehaviour
{
private DirectoryInfo folder;
private StringBuilder stringBuilder;
private void Awake()
{
stringBuilder = new StringBuilder();
try
{
folder = Directory.CreateDirectory("profile" + DateTime.Now.ToString("yy-MM-dd_hh-mm-ss"));
}
catch (Exception e)
{
Debug.LogException(e);
Destroy(this);
return;
}
Profiler.enableBinaryLog = true;
Profiler.enabled = true;
}
private void Update()
{
if (!Application.isPlaying)
{
return;
}
if (Time.frameCount % 300 == 1)
{
stringBuilder.Length = 0;
var fileName = stringBuilder
.Append('_')
.Append(Time.frameCount)
.Append(".prf")
.ToString();
Profiler.enabled = false;
Profiler.enableBinaryLog = false;
var logFile = Path.Combine(folder.FullName, fileName);
Profiler.logFile = logFile;
Profiler.enableBinaryLog = true;
Profiler.enabled = true;
}
}
public void OnDisable()
{
Profiler.enabled = false;
}
public string currentFile;
public int currentFileId = 0;
public string[] allFiles;
#if UNITY_EDITOR
[ContextMenu("Load folder")]
public void LoadFolder()
{
string path = "";
path = EditorUtility.OpenFolderPanel("profiler", "", "");
allFiles = Directory.GetFiles(path, "*.prf");
currentFileId = 0;
AddCurrent();
}
[ContextMenu("Next samples")]
public void Next()
{
currentFileId++;
AddCurrent();
}
[ContextMenu("Prev samples")]
public void Prev()
{
currentFileId--;
AddCurrent();
}
[ContextMenu("Current sample")]
private void AddCurrent()
{
if (allFiles == null || allFiles.Length == 0 || currentFileId < 0 || currentFileId >= allFiles.Length)
{
return;
}
Debug.Log("reading " + allFiles[currentFileId]);
currentFile = string.Format("[{0}] {1}",currentFileId, allFiles[currentFileId]);
Profiler.AddFramesFromFile(allFiles[currentFileId]);
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment