Skip to content

Commit 1326e91

Browse files
authored
fix: Disable screenshot capture in Editor (#2163)
1 parent b4e1b9d commit 1326e91

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Significant change in behavior
6+
7+
- The SDK no longer attaches screenshots when capturing errors in the Unity Editor. ([#2163](https://github.com/getsentry/sentry-unity/pull/2163))
8+
59
### Dependencies
610

711
- Bump Java SDK from v8.11.1 to v8.12.0 ([#2155](https://github.com/getsentry/sentry-unity/pull/2155))

src/Sentry.Unity/ScreenshotEventProcessor.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
using Sentry.Extensibility;
2+
using Sentry.Unity.Integrations;
23
using UnityEngine;
34

45
namespace Sentry.Unity;
56

67
public class ScreenshotEventProcessor : ISentryEventProcessorWithHint
78
{
89
private readonly SentryUnityOptions _options;
10+
private readonly IApplication _application;
11+
public ScreenshotEventProcessor(SentryUnityOptions sentryOptions) : this(sentryOptions, null) { }
912

10-
public ScreenshotEventProcessor(SentryUnityOptions sentryOptions)
13+
internal ScreenshotEventProcessor(SentryUnityOptions sentryOptions, IApplication? application)
1114
{
1215
_options = sentryOptions;
16+
_application = application ?? ApplicationAdapter.Instance;
1317
}
1418

1519
public SentryEvent? Process(SentryEvent @event)
@@ -21,11 +25,18 @@ public ScreenshotEventProcessor(SentryUnityOptions sentryOptions)
2125
{
2226
if (!MainThreadData.IsMainThread())
2327
{
28+
_options.DiagnosticLogger?.LogDebug("Screenshot capture skipped. Can't capture screenshots on other than the main thread.");
2429
return @event;
2530
}
2631

2732
if (_options.BeforeCaptureScreenshotInternal?.Invoke() is not false)
2833
{
34+
if (_application.IsEditor)
35+
{
36+
_options.DiagnosticLogger?.LogInfo("Screenshot capture skipped. Capturing screenshots it not supported in the Editor");
37+
return @event;
38+
}
39+
2940
if (Screen.width == 0 || Screen.height == 0)
3041
{
3142
_options.DiagnosticLogger?.LogWarning("Can't capture screenshots on a screen with a resolution of '{0}x{1}'.", Screen.width, Screen.height);
@@ -37,7 +48,7 @@ public ScreenshotEventProcessor(SentryUnityOptions sentryOptions)
3748
}
3849
else
3950
{
40-
_options.DiagnosticLogger?.LogInfo("Screenshot attachment skipped by BeforeAttachScreenshot callback.");
51+
_options.DiagnosticLogger?.LogInfo("Screenshot capture skipped by BeforeAttachScreenshot callback.");
4152
}
4253

4354

src/Sentry.Unity/ViewHierarchyEventProcessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public ViewHierarchyEventProcessor(SentryUnityOptions sentryOptions)
2626
{
2727
if (!MainThreadData.IsMainThread())
2828
{
29-
_options.DiagnosticLogger?.LogDebug("Can't capture view hierarchy on other than main (UI) thread.");
29+
_options.DiagnosticLogger?.LogDebug("Hierarchy capture skipped. Can't capture hierarchy on other than the main thread.");
3030
return @event;
3131
}
3232

@@ -36,7 +36,7 @@ public ViewHierarchyEventProcessor(SentryUnityOptions sentryOptions)
3636
}
3737
else
3838
{
39-
_options.DiagnosticLogger?.LogInfo("View hierarchy attachment skipped by BeforeAttachViewHierarchy callback.");
39+
_options.DiagnosticLogger?.LogInfo("Hierarchy capture skipped by BeforeAttachViewHierarchy callback.");
4040
}
4141

4242
return @event;

test/Sentry.Unity.Tests/ScreenshotEventProcessorTests.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
using System;
12
using System.IO;
23
using System.Threading;
34
using NUnit.Framework;
5+
using Sentry.Unity.Integrations;
46
using UnityEngine;
7+
using Sentry.Unity.Tests.Stubs;
58

69
namespace Sentry.Unity.Tests;
710

@@ -10,8 +13,9 @@ public class ScreenshotEventProcessorTests
1013
private class Fixture
1114
{
1215
public SentryUnityOptions Options = new() { AttachScreenshot = true };
16+
public TestApplication TestApplication = new();
1317

14-
public ScreenshotEventProcessor GetSut() => new(Options);
18+
public ScreenshotEventProcessor GetSut() => new(Options, TestApplication);
1519
}
1620

1721
private Fixture _fixture = null!;
@@ -42,6 +46,7 @@ public void GetTargetResolution_ReturnsTargetMaxSize(ScreenshotQuality quality,
4246
[Test]
4347
public void Process_IsMainThread_AddsScreenshotToHint()
4448
{
49+
_fixture.TestApplication.IsEditor = false;
4550
var sut = _fixture.GetSut();
4651
var sentryEvent = new SentryEvent();
4752
var hint = new SentryHint();
@@ -72,6 +77,7 @@ public void Process_IsNonMainThread_DoesNotAddScreenshotToHint()
7277
[TestCase(false)]
7378
public void Process_BeforeCaptureScreenshotCallbackProvided_RespectsScreenshotCaptureDecision(bool captureScreenshot)
7479
{
80+
_fixture.TestApplication.IsEditor = false;
7581
_fixture.Options.SetBeforeCaptureScreenshot(() => captureScreenshot);
7682
var sut = _fixture.GetSut();
7783
var sentryEvent = new SentryEvent();
@@ -111,4 +117,22 @@ public void CaptureScreenshot_QualitySetToFull_ScreenshotInFullSize()
111117

112118
Assert.IsTrue(texture.width == testScreenSize && texture.height == testScreenSize);
113119
}
120+
121+
[Test]
122+
[TestCase(true, 0)]
123+
[TestCase(false, 1)]
124+
public void Process_InEditorEnvironment_DoesNotCaptureScreenshot(bool isEditor, int expectedAttachmentCount)
125+
{
126+
// Arrange
127+
_fixture.TestApplication.IsEditor = isEditor;
128+
var sut = _fixture.GetSut();
129+
var sentryEvent = new SentryEvent();
130+
var hint = new SentryHint();
131+
132+
// Act
133+
sut.Process(sentryEvent, hint);
134+
135+
// Assert
136+
Assert.AreEqual(expectedAttachmentCount, hint.Attachments.Count);
137+
}
114138
}

test/SharedClasses/TestApplication.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public TestApplication(
2727
public event Application.LogCallback? LogMessageReceived;
2828
public event Action? Quitting;
2929
public string ActiveSceneName => "TestSceneName";
30-
public bool IsEditor { get; }
30+
public bool IsEditor { get; set; }
3131
public string ProductName { get; }
3232
public string Version { get; }
3333
public string BuildGUID { get; }

0 commit comments

Comments
 (0)