Skip to content

fix(screenshot): restore pause state after composited capture#1230

Open
KamilDev wants to merge 2 commits into
CoplayDev:betafrom
KamilDev:fix/screenshot-restore-pause-state
Open

fix(screenshot): restore pause state after composited capture#1230
KamilDev wants to merge 2 commits into
CoplayDev:betafrom
KamilDev:fix/screenshot-restore-pause-state

Conversation

@KamilDev

@KamilDev KamilDev commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Description

manage_camera action=screenshot in play mode leaves the game paused. The composited capture path added in #1132 pumps the player loop with EditorApplication.Step() to reach the WaitForEndOfFrame capture. Step() pauses play mode as a side effect, and CaptureCompositedAfterFrame never restored the prior state — so a single screenshot silently pauses a running game and never resumes it.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Changes Made

MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs:

  • In CaptureCompositedAfterFrame, record EditorApplication.isPaused before the Step() loop and clear it afterward — unless the game was already paused, in which case the intentional pause is preserved.

Compatibility / Package Source

  • Unity version(s) tested: 6000.3.14f1 (URP, Windows 11)
  • Package source used: file: local reference

Testing/Screenshots/Recordings

  • Not applicable (see below)

Verified live: with the fix, editor_state.play_mode.is_paused stays false after a play-mode manage_camera screenshot include_image=true (the path that pumps Step()); without it, the same call flips is_paused to true and leaves it there. The capture output is unchanged.

Before → After (is_paused after screenshot, running game): truefalse.

Not applicable: play-mode-only editor side-effect restore, verified live (before/after above). The #1132 path that introduced this shipped without an automated test.

Related Issues

Relates to #1132 (introduced the Step() loop this restores state around).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed screenshot capture so it no longer leaves the Unity Editor play mode in an unexpected pause state.
    • Screenshot capture now preserves and restores the prior paused/running state while waiting for the image to complete.

The play-mode composited path drives the player loop with EditorApplication.Step()
to reach WaitForEndOfFrame. Step() pauses play mode as a side effect, and the loop
never restored it — so any play-mode screenshot left the game paused indefinitely
(regression from CoplayDev#1132, which introduced the Step() loop).

Record isPaused before the loop and clear it afterward, unless the game was already
paused (preserve a caller's intentional pause).
@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 095a77b4-5a0a-409f-8b33-2064c051ed75

📥 Commits

Reviewing files that changed from the base of the PR and between 2a4cd4e and 5792e3a.

📒 Files selected for processing (1)
  • MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs
🚧 Files skipped from review as they are similar to previous changes (1)
  • MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs

📝 Walkthrough

Walkthrough

The change updates CaptureCompositedAfterFrame in ScreenshotUtility.cs to preserve the editor pause state while stepping the editor loop for screenshot capture.

Changes

Screenshot Capture Pause Handling

Layer / File(s) Summary
Restore pause state after stepping loop
MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs
CaptureCompositedAfterFrame stores the initial EditorApplication.isPaused value, runs EditorApplication.Step() in a try loop until completion or timeout, and restores isPaused to false only when the game was not already paused.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Possibly related PRs

  • CoplayDev/unity-mcp#1132: Also changes the CaptureCompositedAfterFrame stepping flow in ScreenshotUtility.cs and touches the same pause-state handling path.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely matches the PR’s main fix: restoring pause state after composited screenshot capture.
Description check ✅ Passed The description follows the template well and includes the key sections, change summary, compatibility, testing notes, and related issue.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs (1)

189-197: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Consider wrapping the restore in try/finally.

Correctly captures and conditionally restores the pause state, matching the PR intent. However, if EditorApplication.Step() throws (or something else between capture and restore does), isPaused restoration is skipped — reintroducing the same "stuck paused" symptom this PR fixes.

♻️ Suggested defensive wrap
             bool wasPaused = UnityEditor.EditorApplication.isPaused;
-            for (int i = 0; i < timeoutSteps && !done; i++)
-            {
-                UnityEditor.EditorApplication.Step();
-            }
-            if (!wasPaused)
-                UnityEditor.EditorApplication.isPaused = false;
+            try
+            {
+                for (int i = 0; i < timeoutSteps && !done; i++)
+                {
+                    UnityEditor.EditorApplication.Step();
+                }
+            }
+            finally
+            {
+                if (!wasPaused)
+                    UnityEditor.EditorApplication.isPaused = false;
+            }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs` around lines 189 - 197, The
pause-state restore in ScreenshotUtility’s screenshot stepping logic should be
made exception-safe. In the code around the EditorApplication.Step loop, wrap
the Step() calls and the conditional isPaused restoration in a try/finally so
the original pause state captured in wasPaused is always restored even if Step()
or intervening logic throws.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs`:
- Around line 189-197: The pause-state restore in ScreenshotUtility’s screenshot
stepping logic should be made exception-safe. In the code around the
EditorApplication.Step loop, wrap the Step() calls and the conditional isPaused
restoration in a try/finally so the original pause state captured in wasPaused
is always restored even if Step() or intervening logic throws.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c963cfd2-6977-4dc2-add9-55e0b252aa37

📥 Commits

Reviewing files that changed from the base of the PR and between 8618f83 and 2a4cd4e.

📒 Files selected for processing (1)
  • MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs

Wrap the Step() loop in try/finally so the pause-state restore runs on
every exit path, including if Step() throws — matching the camera-state
restore pattern already used in CaptureFromCameraToProjectFolder.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant