-
-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathSentryScreenshot.cs
More file actions
88 lines (76 loc) · 3.42 KB
/
SentryScreenshot.cs
File metadata and controls
88 lines (76 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using Sentry.Extensibility;
using UnityEngine;
namespace Sentry.Unity;
internal static class SentryScreenshot
{
internal static int GetTargetResolution(ScreenshotQuality quality)
{
return quality switch
{
ScreenshotQuality.High => 1920, // 1080p
ScreenshotQuality.Medium => 1280, // 720p
ScreenshotQuality.Low => 854, // 480p
_ => 854 // Fallback
};
}
public static Texture2D CreateNewScreenshotTexture2D(SentryUnityOptions options) =>
CreateNewScreenshotTexture2D(options, Screen.width, Screen.height);
// For testing
internal static Texture2D CreateNewScreenshotTexture2D(SentryUnityOptions options, int width, int height)
{
// Make sure the screenshot size does not exceed the target size by scaling the image while conserving the
// original ratio based on which, width or height, is the smaller
if (options.ScreenshotQuality is not ScreenshotQuality.Full)
{
var targetResolution = GetTargetResolution(options.ScreenshotQuality);
var ratioW = targetResolution / (float)width;
var ratioH = targetResolution / (float)height;
var ratio = Mathf.Min(ratioH, ratioW);
if (ratio is > 0.0f and < 1.0f)
{
width = Mathf.FloorToInt(width * ratio);
height = Mathf.FloorToInt(height * ratio);
}
}
RenderTexture? renderTextureFull = null;
RenderTexture? renderTextureResized = null;
var previousRenderTexture = RenderTexture.active;
try
{
// Captures the current screenshot synchronously.
var screenshot = new Texture2D(width, height, TextureFormat.RGB24, false);
renderTextureFull = RenderTexture.GetTemporary(Screen.width, Screen.height);
ScreenCapture.CaptureScreenshotIntoRenderTexture(renderTextureFull);
renderTextureResized = RenderTexture.GetTemporary(width, height);
// The image may be mirrored on some platforms - mirror it back.
// See https://docs.unity3d.com/2019.4/Documentation/Manual/SL-PlatformDifferences.html for more info.
// Note, we can't use the `UNITY_UV_STARTS_AT_TOP` macro because it's only available in shaders.
// Instead, there's https://docs.unity3d.com/2019.4/Documentation/ScriptReference/SystemInfo-graphicsUVStartsAtTop.html
if (SentrySystemInfoAdapter.Instance.GraphicsUVStartsAtTop ?? true)
{
Graphics.Blit(renderTextureFull, renderTextureResized, new Vector2(1, -1), new Vector2(0, 1));
}
else
{
Graphics.Blit(renderTextureFull, renderTextureResized);
}
RenderTexture.active = renderTextureResized;
screenshot.ReadPixels(new Rect(0, 0, width, height), 0, 0);
screenshot.Apply();
options.LogDebug("Screenshot captured at {0}x{1}.", width, height);
return screenshot;
}
finally
{
RenderTexture.active = previousRenderTexture;
if (renderTextureFull)
{
RenderTexture.ReleaseTemporary(renderTextureFull);
}
if (renderTextureResized)
{
RenderTexture.ReleaseTemporary(renderTextureResized);
}
}
}
}