Skip to content

Add AttachScreenshot option #284

Closed

Activity

  1. bitsandfoxes commented on Aug 23, 2021

    @bitsandfoxes
    Contributor

    Gotchas to keep in mind:

    • Record at end of frame i.e. a coroutine yield return new WaitForEndOfFrame() and not in the middle of rendering
    • ScreenCapture.CaptureScreenshotAsTexture() returns a Texture2D that can be encoded to .png, .jpg, .tga byte[]
  2. oddgames-david commented on Dec 22, 2021

    @oddgames-david

    Gotchas to keep in mind:

    • Record at end of frame i.e. a coroutine yield return new WaitForEndOfFrame() and not in the middle of rendering
    • ScreenCapture.CaptureScreenshotAsTexture() returns a Texture2D that can be encoded to .png, .jpg, .tga byte[]

    Also:

    • Native screenshot would be better, ensures always receiving a screenshot when the exception is threaded or from a java/objective-c error (so long as it didn't crash)
    • Need to resize it before compressing to jpg, make the max height and compress amount a setting

    This is my current implementation but most of the time I'd love to have screenshots for native exceptions, they are harder to diagnose.

        public class ScreenshotAttachment : IAttachmentContent
        {
    
            public Stream GetStream()
            {
    
                if (!UnityThread.IsRunningOnUnityThread)
                    return new MemoryStream();
    
                float ratio = (float)Screen.width / (float)Screen.height;
    
                int targetHeight = Mathf.Min(Screen.height, 360);
                int targetWidth = Mathf.RoundToInt((float)targetHeight * ratio);
    
                RenderTexture screenshot = RenderTexture.GetTemporary(Screen.width, Screen.height);
    
                ScreenCapture.CaptureScreenshotIntoRenderTexture(screenshot);
                
                // Create a render texture to render into
                RenderTexture screenshotResize = RenderTexture.GetTemporary(targetWidth, targetHeight, 0);
    
                Graphics.Blit(screenshot, screenshotResize);
    
                RenderTexture.ReleaseTemporary(screenshot);
    
                RenderTexture previousActiveRT = RenderTexture.active;
                RenderTexture.active = screenshotResize;
    
                // Create a texture & read data from the active RenderTexture
                Texture2D result = new Texture2D(targetWidth, targetHeight) { name = "Screenshot" };
                result.ReadPixels(new Rect(0, 0, targetWidth, targetHeight), 0, 0);
                result.Apply();
    
                // Reset to initial state
                RenderTexture.active = previousActiveRT;
    
                RenderTexture.ReleaseTemporary(screenshotResize);
    
                var stream = new MemoryStream(result.EncodeToJPG(80));
    
                result.SafeDestroy();
    
                return stream;
            }
    
        }
    
  3. bitsandfoxes commented on Dec 23, 2021

    @bitsandfoxes
    Contributor

    The resizing of the screenshot is a good catch. Thanks for sharing!
    The native screenshot is a good idea and something that's planned to be added.

  4. oddgames-david commented on Mar 16, 2022

    @oddgames-david

    If you allowed attachment streams to be async we could implement screenshots fairly easily ourselves. Or even just give us a callback 1 frame before you actually call GetStream

  5. pistoleta commented on Mar 28, 2022

    @pistoleta

    Gotchas to keep in mind:

    • Record at end of frame i.e. a coroutine yield return new WaitForEndOfFrame() and not in the middle of rendering
    • ScreenCapture.CaptureScreenshotAsTexture() returns a Texture2D that can be encoded to .png, .jpg, .tga byte[]

    Also:

    • Native screenshot would be better, ensures always receiving a screenshot when the exception is threaded or from a java/objective-c error (so long as it didn't crash)
    • Need to resize it before compressing to jpg, make the max height and compress amount a setting

    This is my current implementation but most of the time I'd love to have screenshots for native exceptions, they are harder to diagnose.

        public class ScreenshotAttachment : IAttachmentContent
        {
    
            public Stream GetStream()
            {
    
                if (!UnityThread.IsRunningOnUnityThread)
                    return new MemoryStream();
    
                float ratio = (float)Screen.width / (float)Screen.height;
    
                int targetHeight = Mathf.Min(Screen.height, 360);
                int targetWidth = Mathf.RoundToInt((float)targetHeight * ratio);
    
                RenderTexture screenshot = RenderTexture.GetTemporary(Screen.width, Screen.height);
    
                ScreenCapture.CaptureScreenshotIntoRenderTexture(screenshot);
                
                // Create a render texture to render into
                RenderTexture screenshotResize = RenderTexture.GetTemporary(targetWidth, targetHeight, 0);
    
                Graphics.Blit(screenshot, screenshotResize);
    
                RenderTexture.ReleaseTemporary(screenshot);
    
                RenderTexture previousActiveRT = RenderTexture.active;
                RenderTexture.active = screenshotResize;
    
                // Create a texture & read data from the active RenderTexture
                Texture2D result = new Texture2D(targetWidth, targetHeight) { name = "Screenshot" };
                result.ReadPixels(new Rect(0, 0, targetWidth, targetHeight), 0, 0);
                result.Apply();
    
                // Reset to initial state
                RenderTexture.active = previousActiveRT;
    
                RenderTexture.ReleaseTemporary(screenshotResize);
    
                var stream = new MemoryStream(result.EncodeToJPG(80));
    
                result.SafeDestroy();
    
                return stream;
            }
    
        }
    

    How do you attach this to the event?

  6. pistoleta commented on Mar 28, 2022

    @pistoleta

    If you allowed attachment streams to be async we could implement screenshots fairly easily ourselves. Or even just give us a callback 1 frame before you actually call GetStream

    If BeforeSend would be async we could also set attachments in the context without problems no?

  7. pistoleta commented on Mar 28, 2022

    @pistoleta

    To be honest I never expected Sentry would not have a feature like this... Unity Diagnostics doesn't have 1/10 part of the features you own but does have the screenshot... I dont mean to criticize is just a screenshot of the game just when the exception happened is extremely useful and might save you a lot of time finding where a problem is.

    I hope you guys consider giving some priority to this or a temporary workaround.
    Thanks a lot!

  8. bruno-garcia commented on Mar 29, 2022

    @bruno-garcia
    MemberAuthor

    This is a priority to us. We're adding support for screenshots on iOS and Android, and there's a thumbnail in Sentry's issue detail page.
    The spec for SDK development is here: https://develop.sentry.dev/sdk/features/#screenshots and we plan on adding this to Unity too

  9. oddgames-david commented on Mar 30, 2022

    @oddgames-david

    @pistoleta Here's an updated version that works some of the time. You just add it to your Configure(SentryUnityOptions options) method.

    public class ScreenshotAttachment : IAttachmentContent, UnityEngine.ILogHandler
    {
        private ILogHandler handler;
        private Texture2D screenshot;
    
        public ScreenshotAttachment()
        {
            handler = Debug.unityLogger.logHandler;
            Debug.unityLogger.logHandler = this;
        }
    
        private async Task Capture()
        {
            
            Debug.Log("Capturing screenshot");
    
            await new WaitForEndOfFrame();
    
            float ratio = (float)Screen.width / (float)Screen.height;
    
            int targetHeight = Mathf.Min(Screen.height, 360);
            int targetWidth = Mathf.RoundToInt((float)targetHeight * ratio);
    
            RenderTexture screenshotRt = RenderTexture.GetTemporary(Screen.width, Screen.height);
    
            ScreenCapture.CaptureScreenshotIntoRenderTexture(screenshotRt);
    
            // Create a render texture to render into
            RenderTexture screenshotResize = RenderTexture.GetTemporary(targetWidth, targetHeight, 0);
    
    ##if UNITY_IOS || UNITY_EDITOR
            Graphics.Blit(screenshotRt, screenshotResize, new Vector2(1, -1), new Vector2(0, 1));
    ##else
            Graphics.Blit(screenshotRt, screenshotResize);
    ##endif
            RenderTexture.ReleaseTemporary(screenshotRt);
    
            RenderTexture previousActiveRT = RenderTexture.active;
            RenderTexture.active = screenshotResize;
    
            // Create a texture & read data from the active RenderTexture
            screenshot = new Texture2D(targetWidth, targetHeight) { name = "Screenshot" };
            screenshot.ReadPixels(new Rect(0, 0, targetWidth, targetHeight), 0, 0);
            screenshot.Apply();
        }
    
        public Stream GetStream()
        {
    
    
            if (screenshot == null)
            {
                UnityEngine.Debug.Log("Returning empty stream due to missing screenshot texture2d");
                return new MemoryStream();
            }
    
            var stream = new MemoryStream(screenshot.EncodeToJPG(80));
    
            screenshot.SafeDestroy();
            UnityEngine.Debug.Log("captured screenshot attachment");
            return stream;
        }
    
        public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)
        {
            handler.LogFormat(logType, context, format, args);
        }
    
        public async void LogException(Exception exception, UnityEngine.Object context)
        {
            await Capture();
            handler.LogException(exception,context);
        }
    }

    edit by @vaind: fix the codeblock

  10. self-assigned this
    on Mar 30, 2022
  11. 8 remaining items

  12. oddgames-david commented on Apr 4, 2022

    @oddgames-david

    @vaind I'd watch out for black screenshots when not using

    yield new WaitForEndOfFrame();

  13. pistoleta commented on Apr 27, 2022

    @pistoleta

    I downloaded last release and used the example for sending screenshot but the screenshot seems to be captured upwards.... any hint where the problem might be? I used the exact same code provided in the example.

  14. bitsandfoxes commented on Apr 28, 2022

    @bitsandfoxes
    Contributor

    I downloaded last release and used the example for sending screenshot but the screenshot seems to be captured upwards.... any hint where the problem might be? I used the exact same code provided in the example.

    Which platform did you target?

  15. pistoleta commented on Apr 28, 2022

    @pistoleta

    iOS, btw i had to comment:
    result.SafeDestroy();
    since Texture2D doesn't have that method implemented. What's this line supposed to do, release it from memory?

  16. vaind commented on Apr 28, 2022

    @vaind
    Contributor

    used the example for sending screenshot

    Do you mean the example code, not the new functionality implemented by the latest version? That example is old and doesn't do proper mirroring (necessary for most platforms)

  17. bitsandfoxes commented on Apr 28, 2022

    @bitsandfoxes
    Contributor

    Sorry to see you run into problems.
    Could you open up a new issue detailing what happened, where (platform, Unity-version), and what you had to do to get there/fix it? The Attach Screenshot feature is supposed to work out of the box and you definitely should not have to comment anything to make it work.

  18. bitsandfoxes commented on Apr 28, 2022

    @bitsandfoxes
    Contributor

    If you're updating the Sentry package you also have to update the samples coming with it.

  19. vaind commented on Apr 28, 2022

    @vaind
    Contributor

    Also, there's no result.SafeDestroy(); anywhere in this repository as far as I can tell - what example are you actually referring to?

  20. pistoleta commented on Apr 28, 2022

    @pistoleta

    wrong script 🤦 , seems I already had a ScreenShotAttachement that tried to make work some time ago. Apologies

  21. bitsandfoxes commented on Apr 29, 2022

    @bitsandfoxes
    Contributor

    No worries! Happy to see it worked out for you regardless!

  22. pistoleta commented on Apr 29, 2022

    @pistoleta

    sorry but I dont see any way to attach screenshot in the 0.15.0 at least nothing new in the examples

  23. vaind commented on Apr 29, 2022

    @vaind
    Contributor

    you can enable it in the settings in the Unity UI (Tools->Sentry)

  24. pistoleta commented on Apr 29, 2022

    @pistoleta

    awesome, sorry I didnt see it in the changelog

  25. pistoleta commented on Apr 30, 2022

    @pistoleta

    sorry but I dont see any image attachment, neither from iOS reports or if I send one from unity editor... I enabled from the settings In tools->sentry and also I the SentryOptionsCOnfiguration ScriptableObject.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Feature

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions