CoplayDev/unity-mcp · error · Exception

Preview generation was aborted due to a change of the scene

Error message

Preview generation was aborted due to a change of the scene

What it means

Thrown by TypePreviewGeneratorFromScene.ThrowIfSceneChanged when the CancellationTokenSource has been cancelled. Cancellation is triggered by SceneOpenedDuringGeneration, which fires if the user or code opens a different scene during preview generation. This is a safety abort — the scene swap would corrupt in-progress preview capture.

Source

Thrown at TestProjects/AssetStoreUploads/Packages/com.unity.asset-store-tools/Editor/Previews/Scripts/Generators/Custom/TypeGenerators/TypePreviewGeneratorFromScene.cs:63

                EditorSceneManager.sceneOpened -= SceneOpenedDuringGeneration;
                _cancellationTokenSource.Dispose();
                if (!string.IsNullOrEmpty(originalScenePath))
                    EditorSceneManager.OpenScene(originalScenePath);
            }
        }

        protected abstract Task<List<PreviewMetadata>> GeneratePreviewsInScene(IEnumerable<UnityEngine.Object> assets);

        private void SceneOpenedDuringGeneration(Scene _, OpenSceneMode __)
        {
            if (!_cancellationTokenSource.IsCancellationRequested)
                _cancellationTokenSource.Cancel();
        }

        protected void ThrowIfSceneChanged()
        {
            if (_cancellationTokenSource.Token.IsCancellationRequested)
                throw new Exception("Preview generation was aborted due to a change of the scene");
        }

        protected Shader GetDefaultObjectShader()
        {
            switch (RenderPipelineUtility.GetCurrentPipeline())
            {
                case RenderPipeline.BiRP:
                    return Shader.Find("Standard");
                case RenderPipeline.URP:
                    return Shader.Find("Universal Render Pipeline/Lit");
                case RenderPipeline.HDRP:
                    return Shader.Find("HDRP/Lit");
                default:
                    throw new NotImplementedException("Undefined Render Pipeline");
            }
        }

        protected Shader GetDefaultParticleShader()

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Do not change scenes while preview generation is in progress — wait for it to complete.
  2. Wrap the Generate() call in a try-catch for this specific exception and restart generation if interrupted.
  3. Lock scene switching in the editor (or show a progress bar with 'Cancel' instead of allowing manual scene changes) during generation.
  4. If automating, ensure no concurrent scene operations run alongside preview generation.

Example fix

// before
var previews = await generator.Generate();
// unhandled exception if scene changes mid-generation

// after
try
{
    var previews = await generator.Generate();
}
catch (Exception ex) when (ex.Message.Contains("aborted due to a change of the scene"))
{
    ASDebug.LogWarning("Preview generation interrupted by scene change. Retrying...");
    previews = await generator.Generate();
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Prevent scene changes during generation by disabling scene operations
// (no pure validation possible — the cancellation is triggered by an external event)
// Best practice: show a modal progress bar that blocks user interaction
EditorUtility.DisplayProgressBar("Preview Generation", "Generating previews...", 0f);
try
{
    await generator.Generate();
}
finally
{
    EditorUtility.ClearProgressBar();
}

Try / catch

try
{
    previews = await generator.Generate();
}
catch (Exception ex) when (ex.Message.Contains("aborted due to a change of the scene"))
{
    ASDebug.LogWarning("Preview generation interrupted by scene change. Please avoid switching scenes during generation.");
    // Optionally retry after re-opening the preview scene
    await PreviewSceneUtility.OpenPreviewSceneForCurrentPipeline();
    previews = await generator.Generate();
}

Prevention

When it happens

Trigger: During GeneratePreviewsInScene execution, EditorSceneManager.sceneOpened fires (user opens/loads a scene), which calls _cancellationTokenSource.Cancel(). A subsequent ThrowIfSceneChanged() call observes the cancelled token and throws.

Common situations: User manually opens a different scene while preview generation is running; another editor script triggers scene loading during the async generation; automated test harness switches scenes; scene was double-clicked in Project window mid-generation.

Related errors


AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13). Data as JSON: /api/errors/3b11dc42061b8b4c. Report an issue: GitHub.