Skip to content

Instantly share code, notes, and snippets.

@babon
Created May 18, 2023 14:14
Show Gist options
  • Save babon/7f6c0095b5096d2b13f9998e8153bbf7 to your computer and use it in GitHub Desktop.
Save babon/7f6c0095b5096d2b13f9998e8153bbf7 to your computer and use it in GitHub Desktop.
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<string, Stopwatch> Dictionary;
#if UNITY_EDITOR
static CompilationTimeTracker()
{
if (!EditorApplication.isPlayingOrWillChangePlaymode)
{
if (!EditorPrefs.GetBool(PlayerPrefsName, false)) return;
CompilationPipeline.assemblyCompilationStarted += OnAssemblyCompilationStarted;
CompilationPipeline.assemblyCompilationFinished += OnAssemblyCompilationFinished;
Dictionary = new Dictionary<string, Stopwatch>();
}
}
#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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment