Skip to content

Instantly share code, notes, and snippets.

@valyard
Created November 24, 2017 14:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valyard/2cab7d8e0eb42afc54b2a7519f67e686 to your computer and use it in GitHub Desktop.
Save valyard/2cab7d8e0eb42afc54b2a7519f67e686 to your computer and use it in GitHub Desktop.
Record profiling data split by files and with frame time markers added.
using UnityEngine;
using UnityEngine.Profiling;
using System.IO;
using UnityEngine.SceneManagement;
using System.Text;
using System;
public class ProfilerRecorder : MonoBehaviour
{
public const string START_MARKER = "PROFILER_DATA_START";
public const int FRAMES_PER_BATCH = 300;
public int StartFrame = 0;
public int FramesToRecord = 3000;
public bool SplitFrames = false;
private bool started = false;
private int frame = 0;
private int batch = -1;
private long startTick;
private void Start()
{
if (!Profiler.supported) enabled = false;
sb = new StringBuilder();
startTick = DateTime.Now.Ticks;
}
private StringBuilder sb;
private void Update()
{
if (started)
{
sb.Length = 0;
sb.Append("$$");
sb.Append(Time.frameCount.ToString("X"));
sb.Append(" ");
sb.Append((DateTime.Now.Ticks - startTick).ToString("X"));
sb.Append(" ");
sb.Append(((int)(Time.deltaTime * TimeSpan.TicksPerSecond)).ToString("X"));
Profiler.BeginSample(sb.ToString());
Profiler.EndSample();
frame++;
if (frame >= FramesToRecord)
{
stopRecording();
return;
}
if (SplitFrames && (frame % FRAMES_PER_BATCH == 0))
{
startRecording();
}
}
else if (Time.frameCount >= StartFrame)
{
Profiler.enabled = true;
startRecording();
started = true;
Profiler.BeginSample(START_MARKER);
Profiler.EndSample();
}
}
private void startRecording()
{
batch++;
Profiler.logFile = Path.Combine(Application.persistentDataPath,
string.Format("profiler_{0}_{1}", Application.unityVersion, batch));
Debug.Log(Profiler.logFile);
Profiler.enableBinaryLog = true;
}
private void stopRecording()
{
Profiler.enableBinaryLog = false;
Profiler.logFile = null;
Profiler.enabled = false;
enabled = false;
SceneManager.LoadScene("Empty");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment