Created
March 24, 2021 09:41
-
-
Save Unity-Javier/ea9e91d01e1e9ed88c236a4269e49488 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Threading; | |
using Unity.Profiling; | |
using UnityEditor; | |
using UnityEditor.Experimental; | |
using UnityEditor.Profiling; | |
using UnityEditorInternal; | |
using UnityEngine; | |
using UnityEngine.Profiling; | |
public class RefreshProfilerHelper : AssetsModifiedProcessor | |
{ | |
protected override void OnAssetsModified(string[] changedAssets, string[] addedAssets, string[] deletedAssets, AssetMoveInfo[] movedAssets) | |
{ | |
//use this file for after domain reload | |
const string baseDir = "Assets/../RefreshProfilerHelper"; | |
var refreshStartedFile = Path.Combine(baseDir, "refreshprofilferhelper_started.txt"); | |
if(!Directory.Exists(baseDir)) | |
Directory.CreateDirectory(baseDir); | |
File.WriteAllText(refreshStartedFile, "started"); | |
ProfilerDriver.enabled = true; | |
ProfilerDriver.profileEditor = true; | |
} | |
public class RefreshProfilerPostProcessor : AssetPostprocessor | |
{ | |
private static int mDisableProfilerTicker = 0; | |
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, | |
string[] movedFromAssetPaths) | |
{ | |
const string baseDir = "Assets/../RefreshProfilerHelper"; | |
var refreshStartedFile = Path.Combine(baseDir, "refreshprofilferhelper_started.txt"); | |
if (File.Exists(refreshStartedFile)) | |
{ | |
File.Delete(refreshStartedFile); | |
ProfilerDriver.ClearAllFrames(); | |
ProfilerDriver.NewProfilerFrameRecorded += OnNewProfilerFrameRecorder; | |
mDisableProfilerTicker = 0; | |
} | |
} | |
private static void OnNewProfilerFrameRecorder(int connectionId, int newFrameIndex) | |
{ | |
using (var frameData = ProfilerDriver.GetHierarchyFrameDataView(newFrameIndex, 0, | |
HierarchyFrameDataView.ViewModes.Default, HierarchyFrameDataView.columnDontSort, false)) | |
{ | |
Debug.Log($"Frame index {newFrameIndex}, frame time: {frameData.frameTimeMs}"); | |
const float frameTimeThreshold = 1000.0f; //change this value to filter more captures | |
if (frameData.frameTimeMs > frameTimeThreshold) | |
{ | |
const string baseDir = "Assets/../RefreshProfilerHelper"; | |
var refreshID = AssetDatabaseExperimental.counters.import.refresh.total; | |
var now = DateTime.Now; | |
//Make the file name somewhat unique | |
var dateString = $"{now.Year}_{now.Month}_{now.Day}"; | |
var filePath = Path.Combine(baseDir, | |
$"refresh_{refreshID}_frame_{newFrameIndex}_date_{dateString}.data"); | |
ProfilerDriver.SaveProfile(filePath); | |
Debug.Log($"Profiler capture saved to {filePath}"); | |
EditorApplication.delayCall += DisableProfilerNextTick; | |
} | |
} | |
EditorApplication.delayCall += DisableProfilerNextTick; | |
} | |
//Wait 10 frames, sometimes it takes some time for the profiler to | |
//get all the data | |
private static void DisableProfilerNextTick() | |
{ | |
const int maxFramesToWait = 50; | |
if (mDisableProfilerTicker < maxFramesToWait) | |
{ | |
EditorApplication.delayCall += DisableProfilerNextTick; | |
mDisableProfilerTicker++; | |
return; | |
} | |
ProfilerDriver.enabled = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment