-
Notifications
You must be signed in to change notification settings - Fork 652
fix: ScreenshotUtility.cs WebGL compilation #578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
ScreenCapture doesn't exist in WebGL scripting backend, causing CS0103 error when WebGL is the active build target. Editor scripts compile with desktop backend regardless of target platform. No functionality impact since MCP tools only run in Editor.
Reviewer's guide (collapsed on small PRs)Reviewer's GuideMoves ScreenshotUtility into an Editor-only context to avoid WebGL compilation errors by changing its namespace and references, and removes the now-unneeded runtime helpers folder metadata. Class diagram for ScreenshotUtility editor migrationclassDiagram
namespace MCPForUnity_Runtime_Helpers {
class ScreenshotUtility
class ScreenshotCaptureResult
}
namespace MCPForUnity_Editor_Helpers {
class ScreenshotUtility
class ScreenshotCaptureResult
}
namespace MCPForUnity_Editor_Tools {
class ManageScene
}
ManageScene ..> ScreenshotUtility : uses
MCPForUnity_Runtime_Helpers_ScreenshotUtility <|.. MCPForUnity_Editor_Helpers_ScreenshotUtility
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
📝 WalkthroughWalkthroughScreenshotUtility is relocated from the Runtime namespace to the Editor namespace to resolve WebGL compilation errors. This involves updating the namespace declaration in ScreenshotUtility.cs, removing the obsolete Runtime import from ManageScene.cs, and deleting the associated Runtime metadata file. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey - I've left some high level feedback:
- The comment above
ScreenshotCaptureResultstill refers to avoiding editor-only dependencies in runtime code, which no longer matches the class’s location/role and should be updated or removed for clarity. - In
ManageScene.cs, the inline comment// For ScreenshotUtilitywas removed along with the using, but the remaining// For Response classcomment could be clarified or generalized now that this file imports multiple helpers from the same namespace.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The comment above `ScreenshotCaptureResult` still refers to avoiding editor-only dependencies in runtime code, which no longer matches the class’s location/role and should be updated or removed for clarity.
- In `ManageScene.cs`, the inline comment `// For ScreenshotUtility` was removed along with the using, but the remaining `// For Response class` comment could be clarified or generalized now that this file imports multiple helpers from the same namespace.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
MCPForUnity/Editor/Helpers/ScreenshotUtility.cs (1)
97-115: Texture2D is never destroyed — memory leak.The
Texture2Dallocated at line 103 is not disposed after encoding to PNG. Since this is Editor code, useDestroyImmediateto release the texture memory.🐛 Proposed fix
try { camera.targetTexture = rt; camera.Render(); RenderTexture.active = rt; var tex = new Texture2D(width, height, TextureFormat.RGBA32, false); - tex.ReadPixels(new Rect(0, 0, width, height), 0, 0); - tex.Apply(); - - byte[] png = tex.EncodeToPNG(); - File.WriteAllBytes(normalizedFullPath, png); + try + { + tex.ReadPixels(new Rect(0, 0, width, height), 0, 0); + tex.Apply(); + + byte[] png = tex.EncodeToPNG(); + File.WriteAllBytes(normalizedFullPath, png); + } + finally + { + UnityEngine.Object.DestroyImmediate(tex); + } } finally {
🤖 Fix all issues with AI agents
In `@MCPForUnity/Editor/Helpers/ScreenshotUtility.cs`:
- Around line 6-7: The inline comment following the namespace
MCPForUnity.Editor.Helpers is stale—remove or rewrite it to reflect that this
file (ScreenshotUtility) now lives in the Editor assembly and is Editor-only;
update the comment text to mention "Editor-only utilities" or delete the comment
entirely so it no longer references "Runtime Utilities" or "runtime code", and
ensure the change is applied in the ScreenshotUtility.cs header near the
namespace declaration.
| namespace MCPForUnity.Editor.Helpers | ||
| //The reason for having another Runtime Utilities in additional to Editor Utilities is to avoid Editor-only dependencies in this runtime code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stale comment references Runtime context.
The namespace update on line 6 is correct. However, line 7's comment still refers to "Runtime Utilities" and "runtime code," which is now misleading since this file has been relocated to the Editor assembly.
📝 Suggested fix
namespace MCPForUnity.Editor.Helpers
-//The reason for having another Runtime Utilities in additional to Editor Utilities is to avoid Editor-only dependencies in this runtime code.
{Or update the comment to reflect the new Editor-only purpose if documentation is still desired.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| namespace MCPForUnity.Editor.Helpers | |
| //The reason for having another Runtime Utilities in additional to Editor Utilities is to avoid Editor-only dependencies in this runtime code. | |
| namespace MCPForUnity.Editor.Helpers | |
| { |
🤖 Prompt for AI Agents
In `@MCPForUnity/Editor/Helpers/ScreenshotUtility.cs` around lines 6 - 7, The
inline comment following the namespace MCPForUnity.Editor.Helpers is
stale—remove or rewrite it to reflect that this file (ScreenshotUtility) now
lives in the Editor assembly and is Editor-only; update the comment text to
mention "Editor-only utilities" or delete the comment entirely so it no longer
references "Runtime Utilities" or "runtime code", and ensure the change is
applied in the ScreenshotUtility.cs header near the namespace declaration.
Problem
When WebGL is the active build target, the project fails to compile:
error CS0103: The name 'ScreenCapture' does not exist in the current context
Solution
Move
ScreenshotUtility.csfromRuntime/Helpers/toEditor/Helpers/.Why this works
ScreenCapture doesn't exist in WebGL scripting backend. Editor scripts compile with desktop backend regardless of target platform. No functionality impact since MCP tools only run in Editor.
Fixes #574
Summary by Sourcery
Move screenshot capture utilities into the editor-only helpers namespace to avoid WebGL build errors.
Bug Fixes:
Enhancements:
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.