Skip to content

Instantly share code, notes, and snippets.

@valyard
Created November 6, 2017 01:06
Show Gist options
  • Save valyard/9ad133828c0f40d1bbcb46ab5e57999e to your computer and use it in GitHub Desktop.
Save valyard/9ad133828c0f40d1bbcb46ab5e57999e to your computer and use it in GitHub Desktop.
Export current profiler frame to Chrome Trace Event Format
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System;
using System.Text;
using System.IO;
public class TestWindow : EditorWindow
{
[MenuItem("Tools/Test Window")]
public static void CreateWindow()
{
var wnd = EditorWindow.GetWindow<TestWindow>();
wnd.Show();
}
private void OnGUI()
{
if (GUILayout.Button("Export Frame"))
{
var iter = new ProfilerFrameDataIterator();
var threads = iter.GetThreadCount(ProfilerDriver.lastFrameIndex);
var data = new ProfilerData();
data.otherData.frameTime = iter.frameTimeMS;
for (var i = 0; i < threads; i++)
{
iter.SetRoot(ProfilerDriver.lastFrameIndex, i);
if (!iter.Next(true)) continue;
do
{
var item = ProfilerItem.Create(iter);
item.tid = i;
data.Add(item);
} while (iter.Next(true));
}
iter.Dispose();
var json = JsonUtility.ToJson(data);
File.WriteAllText(Path.Combine(Application.dataPath, "../Traces/profiler.json"), json);
}
}
[Serializable]
private class ProfilerData
{
public List<ProfilerItem> traceEvents = new List<ProfilerItem>();
public ProfilerOtherData otherData = new ProfilerOtherData();
public void Add(ProfilerItem item)
{
traceEvents.Add(item);
}
}
[Serializable]
private class ProfilerOtherData
{
public float frameTime;
}
[Serializable]
private class ProfilerItem
{
public int pid = 1;
public int tid = 1;
public string ph = "X";
public int ts;
public int dur;
public string name;
public ProfilerItemArgs args;
public static ProfilerItem Create(ProfilerFrameDataIterator iter)
{
var item = new ProfilerItem() {name = iter.name, dur = (int)(iter.durationMS * 1000), ts = (int)(iter.startTimeMS * 1000)};
item.args = new ProfilerItemArgs() {path = iter.path, instanceId = iter.instanceId, sampleId = iter.sampleId};
return item;
}
}
[Serializable]
private class ProfilerItemArgs
{
public string path;
public int instanceId;
public int sampleId;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment