Search Unity

[SOLVED] Problems with taking a screenshot in LWRP

Discussion in 'Scripting' started by the_mr_matt, Mar 24, 2019.

  1. the_mr_matt

    the_mr_matt

    Joined:
    Jul 21, 2015
    Posts:
    124
    I'm trying to capture some screenshots for my game. They need to be saved as jpgs to a folder, latency is not an issue, I don't care if it happens over 10 seconds. My number one priority is to keep the game at a steady framerate.

    Some things I've tried:
    I found a LWRP example here: https://github.com/UnityTechnologies/LWRPScriptableRenderPass_ExampleLibrary
    However, this is just used for image effects. I have no idea how to take the contents of the screen and put it into an image file.

    The docs for LWRP kinda suck and the examples I've found aren't well commented. To say that I have no idea what I'm doing in SRP is an understatement. Pls help
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    How slow is the actual ReadPixels call in comparison to the encoding and saving part? You could write a few hundred bytes to the file every frame in a coroutine if the the ReadPixels call is quick enough.

    I'm also not sure that players really expect the game to maintain 60 FPS while taking screenshots, so is that aspect really necessary?
     
  3. the_mr_matt

    the_mr_matt

    Joined:
    Jul 21, 2015
    Posts:
    124
    ScreenCapture.CaptureScreenshot took ~200ms.

    ReadPixels() is 40-70ms.
    Texture.Apply() is ~5ms.
    EncodeToJPG() is 35-50ms.
    GetRawTextureData() is ~10ms.
    File.WriteAllBytes() is <1ms.

    All up, the render texture method was about half the speed of ScreenCapture.CaptureScreenshot, but that's pretty meaningless because both are way too slow.

    ReadPixels has to be done all at once because if I split that over several frames the pixels will have all changed. EncodeToJPG can't be split over multiple frames. And File.WriteAllBytes is fast enough as it is. Also, unity isn't thread safe so the only thing in all this that I could dump on another thread is File.WriteAllBytes, but again, that's already fast enough as it is. GetRawTextureData is slightly faster than encoding, but that's a bit useless because then I can't actually see the image.

    I need this to be taking screenshots in the background about every 10-15s, it'll just overwrite old files leaving a maximum of about 5-10 screenshots at any time. Right now the game has very very noticeable stutters. Not ideal.
     
    Last edited: Mar 25, 2019
  4. the_mr_matt

    the_mr_matt

    Joined:
    Jul 21, 2015
    Posts:
    124