Last active
April 2, 2025 23:06
-
-
Save karljj1/9c6cce803096b5cd4511cf0819ff517b to your computer and use it in GitHub Desktop.
Find out what assemblies are being built and how long each takes.
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.Generic; | |
using System.Text; | |
using UnityEditor; | |
using UnityEditor.Compilation; | |
using UnityEngine; | |
[InitializeOnLoad] | |
public class AsmdefDebug | |
{ | |
const string AssemblyReloadEventsEditorPref = "AssemblyReloadEventsTime"; | |
const string AssemblyCompilationEventsEditorPref = "AssemblyCompilationEvents"; | |
static readonly int ScriptAssembliesPathLen = "Library/ScriptAssemblies/".Length; | |
static Dictionary<string, DateTime> s_StartTimes = new Dictionary<string, DateTime>(); | |
static StringBuilder s_BuildEvents = new StringBuilder(); | |
static double s_CompilationTotalTime; | |
static AsmdefDebug() | |
{ | |
CompilationPipeline.assemblyCompilationStarted += CompilationPipelineOnAssemblyCompilationStarted; | |
CompilationPipeline.assemblyCompilationFinished += CompilationPipelineOnAssemblyCompilationFinished; | |
AssemblyReloadEvents.beforeAssemblyReload += AssemblyReloadEventsOnBeforeAssemblyReload; | |
AssemblyReloadEvents.afterAssemblyReload += AssemblyReloadEventsOnAfterAssemblyReload; | |
} | |
static void CompilationPipelineOnAssemblyCompilationStarted(string assembly) | |
{ | |
s_StartTimes[assembly] = DateTime.UtcNow; | |
} | |
static void CompilationPipelineOnAssemblyCompilationFinished(string assembly, CompilerMessage[] arg2) | |
{ | |
var time = s_StartTimes[assembly]; | |
var timeSpan = DateTime.UtcNow - s_StartTimes[assembly]; | |
s_CompilationTotalTime += timeSpan.TotalMilliseconds; | |
s_BuildEvents.AppendFormat("{0:0.00}s {1}\n", timeSpan.TotalMilliseconds / 1000f, assembly.Substring(ScriptAssembliesPathLen, assembly.Length - ScriptAssembliesPathLen)); | |
} | |
static void AssemblyReloadEventsOnBeforeAssemblyReload() | |
{ | |
s_BuildEvents.AppendFormat("compilation total: {0:0.00}s\n", s_CompilationTotalTime / 1000f); | |
EditorPrefs.SetString(AssemblyReloadEventsEditorPref, DateTime.UtcNow.ToBinary().ToString()); | |
EditorPrefs.SetString(AssemblyCompilationEventsEditorPref, s_BuildEvents.ToString()); | |
} | |
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"); | |
} | |
} | |
} | |
} |
Again, I said when the project is loaded, therefore the profiler can not be used. And as I said my project takes about 4secs to load and yet this code reports it takes anywhere between 8mins to 16 mins, it kind of looks like it is not clearing the times and just adding them to previous project loads.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The profiler can help to tell you why it's taking a long time. Some scripts will execute during the reload and this can cause a significant delay. It may be a package or tool you are using causing the problems, the profiler can help to identify this.