# compilationStarted

> An event that is invoked on the main thread when the compilation of assemblies starts.

## Definition

* **Type:** Event
* **Namespace:** [UnityEditor.Compilation](/engine/6000.7/script-reference/unityeditor/compilation.md)
* **Assembly:** UnityEditor.CoreModule

```csharp
public static event Action<object> compilationStarted
```

### Remarks

`CompilationPipeline.compilationStarted` is called before the first [CompilationPipeline.assemblyCompilationStarted](/engine/6000.7/script-reference/unityeditor/compilation/compilationpipeline/assemblycompilationstarted.md) event.

You can use the `context` to match `CompilationPipeline.compilationStarted` and [CompilationPipeline.compilationFinished](/engine/6000.7/script-reference/unityeditor/compilation/compilationpipeline/compilationfinished.md) events that belong to the same compilation cycle. Compilation start and finish events from the same compilation cycle have the same `context` instance. Treat the `context` only as an identifier: compare by reference or use it as a dictionary key, but don't assume any structure or string representation.

If multiple compilations happen concurrently or in quick succession, you can use the `context` to associate timing, logging, error counts, or any per-compilation data with the right compilation cycle. For example, on compilation start, you can create an entry referenced by the `context` as a key, and capture start time or relevant state. On compilation finish, you can then look up the same entry via the `context`, compute duration, summarize results, and clean up.

### Examples

```csharp
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Compilation;

[InitializeOnLoad]
public static class CompilationTracker
{
    // Store per-compilation data keyed by the context object
    private static readonly Dictionary<object, CompilationInfo> _compilations = new();

    static CompilationTracker()
    {
        CompilationPipeline.compilationStarted += OnCompilationStarted;
        CompilationPipeline.compilationFinished += OnCompilationFinished;
    }

    private static void OnCompilationStarted(object context)
    {
        _compilations[context] = new CompilationInfo
        {
            StartTime = DateTime.UtcNow,
            TriggerSummary = "Scripts changed or domain reload pending" // whatever you want to record
        };

        // Optional: log or prepare resources
        UnityEngine.Debug.Log("Compilation started");
    }

    private static void OnCompilationFinished(object context)
    {
        if (_compilations.TryGetValue(context, out var info))
        {
            var duration = DateTime.UtcNow - info.StartTime;

            // You can also inspect CompilationPipeline.GetAssemblies() or other APIs here
            UnityEngine.Debug.Log($"Compilation finished in {duration.TotalSeconds:F2}s");

            // Clean up
            _compilations.Remove(context);
        }
        else
        {
            // In case something slipped through, avoid leaking state
            UnityEngine.Debug.LogWarning("Compilation finished, but no matching context found.");
        }
    }

    private class CompilationInfo
    {
        public DateTime StartTime;
        public string TriggerSummary;
    }
}
```
