Skip to content

Commit

Permalink
fix: handle non-main thread screenshot attachment explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Apr 4, 2022
1 parent 6b3c33b commit c5ff9d6
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Sentry.Unity/ScreenshotAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public ScreenshotAttachmentContent(SentryUnityOptions options, SentryMonoBehavio

public Stream GetStream()
{
// Note: we need to check explicitly that we're on the same thread. While Unity would throw otherwise
// when capturing the screenshot, it would only do so on development builds. On release, it just crashes...
if (!_behaviour.MainThreadData.IsMainThread())
{
_options.DiagnosticLogger?.LogDebug("Won't capture screenshot because we're not on the main thread");
// Throwing here to avoid empty attachment being sent to Sentry.
// return new MemoryStream();
throw new Exception("Sentry: cannot capture screenshot attachment on other than the main (UI) thread.");
}

// Calculate the desired size by calculating the ratio between the desired height/width and the actual one,
// and than resizing based on the smaller of the two ratios.
var width = Screen.width;
Expand All @@ -41,7 +51,6 @@ public Stream GetStream()
}

// Captures the current screenshot synchronously.
// Throws if not on the UI thread - sentry-dotnet skips the attachment in that case.
var screenshot = new Texture2D(width, height, TextureFormat.RGB24, false);
var rtFull = RenderTexture.GetTemporary(Screen.width, Screen.height);
ScreenCapture.CaptureScreenshotIntoRenderTexture(rtFull);
Expand Down

0 comments on commit c5ff9d6

Please sign in to comment.