Last active
July 18, 2019 20:20
-
-
Save AlexMerzlikin/8e920fad3a451127b19a7cb8fb512437 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
#if UNITY_EDITOR | |
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using UnityEditor; | |
using UnityEditor.Compilation; | |
using UnityEngine; | |
[InitializeOnLoad] | |
public class AsmDefDebug | |
{ | |
private const string AssemblyReloadEventsEditorPref = "AssemblyReloadEventsTime"; | |
private const string AssemblyCompilationEventsEditorPref = "AssemblyCompilationEvents"; | |
private static readonly int ScriptAssembliesPathLen = "Library/ScriptAssemblies/".Length; | |
private static readonly Dictionary<string, DateTime> StartTimes = new Dictionary<string, DateTime>(); | |
private static readonly StringBuilder EventsStringBuilder = new StringBuilder(); | |
private static double CompilationTotalTime; | |
private static AsmDefDebug() | |
{ | |
CompilationPipeline.assemblyCompilationStarted += CompilationPipelineOnAssemblyCompilationStarted; | |
CompilationPipeline.assemblyCompilationFinished += CompilationPipelineOnAssemblyCompilationFinished; | |
AssemblyReloadEvents.beforeAssemblyReload += AssemblyReloadEventsOnBeforeAssemblyReload; | |
AssemblyReloadEvents.afterAssemblyReload += AssemblyReloadEventsOnAfterAssemblyReload; | |
} | |
private static void CompilationPipelineOnAssemblyCompilationStarted(string assembly) | |
{ | |
StartTimes[assembly] = DateTime.UtcNow; | |
} | |
private static void CompilationPipelineOnAssemblyCompilationFinished(string assembly, CompilerMessage[] arg2) | |
{ | |
var time = StartTimes[assembly]; | |
var timeSpan = DateTime.UtcNow - time; | |
compilationTotalTime += timeSpan.TotalMilliseconds; | |
EventsStringBuilder.Append($"{timeSpan.TotalMilliseconds / 1000f:0.00}s {assembly.Substring(ScriptAssembliesPathLen, assembly.Length - ScriptAssembliesPathLen)}\n"); | |
} | |
private static void AssemblyReloadEventsOnBeforeAssemblyReload() | |
{ | |
EventsStringBuilder.Append($"compilation total: {compilationTotalTime / 1000f:0.00}s\n"); | |
EditorPrefs.SetString(AssemblyReloadEventsEditorPref, DateTime.UtcNow.ToBinary().ToString()); | |
EditorPrefs.SetString(AssemblyCompilationEventsEditorPref, EventsStringBuilder.ToString()); | |
} | |
private static void AssemblyReloadEventsOnAfterAssemblyReload() | |
{ | |
var binString = EditorPrefs.GetString(AssemblyReloadEventsEditorPref); | |
long bin = 0; | |
if (long.TryParse(binString, out bin)) | |
{ | |
var date = DateTime.FromBinary(bin); | |
var time = DateTime.UtcNow - date; | |
var compilationTimes = EditorPrefs.GetString(AssemblyCompilationEventsEditorPref); | |
if (!string.IsNullOrEmpty(compilationTimes)) | |
{ | |
Debug.Log($"Compilation Report\n{compilationTimes} Assembly Reload Time: {time.TotalSeconds}s\n"); | |
} | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment