using System.Collections.Generic; using System.Diagnostics; using UnityEditor; using UnityEditor.Compilation; using Debug = UnityEngine.Debug; [InitializeOnLoad] internal static class CompilationTimeTracker { private static readonly Dictionary Dictionary; #if UNITY_EDITOR static CompilationTimeTracker() { if (!EditorApplication.isPlayingOrWillChangePlaymode) { if (!EditorPrefs.GetBool(PlayerPrefsName, false)) return; CompilationPipeline.assemblyCompilationStarted += OnAssemblyCompilationStarted; CompilationPipeline.assemblyCompilationFinished += OnAssemblyCompilationFinished; Dictionary = new Dictionary(); } } #endif private static void OnAssemblyCompilationStarted(string value) { if (!EditorPrefs.GetBool(PlayerPrefsName, false)) return; if (Dictionary.ContainsKey(value)) Dictionary[value] = Stopwatch.StartNew(); else Dictionary.Add(value, Stopwatch.StartNew()); } private static void OnAssemblyCompilationFinished(string value, CompilerMessage[] messages) { if (!EditorPrefs.GetBool(PlayerPrefsName, false)) return; Dictionary[value].Stop(); Debug.Log(/*Assembly */$"{value.Replace("Library/ScriptAssemblies/", "")} built in {Dictionary[value].Elapsed.TotalSeconds:F} seconds."); } const string MenuName = "Window/CompilationTimeTracker"; const string PlayerPrefsName = "EditorCompilationTimeTracker"; public static bool IsEnabled { get => EditorPrefs.GetBool(PlayerPrefsName, true); set => EditorPrefs.SetBool(PlayerPrefsName, value); } [MenuItem(MenuName/*, priority = 999999*/)] private static void ToggleAction() { IsEnabled = !IsEnabled; if (Dictionary != null) Dictionary.Clear(); } [MenuItem(MenuName, true/*, priority = 999999*/)] private static bool ToggleActionValidate() { Menu.SetChecked(MenuName, IsEnabled); return true; } }