Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Unity 2017.3.0f3 bug - ScreenCapture.CaptureScreenshotAsTexture() converts as transparent to sprite

Discussion in 'Documentation' started by ina, Jan 2, 2018.

  1. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    686
    Not sure if I've found a Unity 2017.3.0f3 bug after a few hours of debugging where the uncertainty turned out to be how this new screenshot to texture method does not produce a sprite that loads on a UI Image -

    1. Texture2D tex = ScreenCapture.CaptureScreenshotAsTexture();
    2. Assign that tex to a uiimage.sprite
    3. Note that the UI image sprite disappears...
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    3,020
    Can you post the full code so I can test it?
     
  3. jdessart_current

    jdessart_current

    Joined:
    Jun 23, 2017
    Posts:
    7
    I'm seeing this as well, but with a RawImage. The area covered by the RawImage is completely transparent, showing the camera's clear instead of an image. But when I use ImageConversion to write it as a PNG, the image is fine.

    I'll try to make a sample.

    Edit: This is on an iOS 11 beta, iPhone 6S.
     
    juanitogan likes this.
  4. jdessart_current

    jdessart_current

    Joined:
    Jun 23, 2017
    Posts:
    7
    Here you go. This recreates the problem on iOS, and when run in the macOS editor.
     

    Attached Files:

  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    3,020
    Hi. Could you please file a bug report with this? I don't have any access to iOS at the moment.
     
  6. jdessart_current

    jdessart_current

    Joined:
    Jun 23, 2017
    Posts:
    7
    Done. I hope it helps!
     
  7. juanitogan

    juanitogan

    Joined:
    Aug 18, 2017
    Posts:
    14
    I ran into this same issue on Windows with Unity 2017.4.0f1. Issue 991764 is reported as fixed in 2018.2.

    Following the lead of @jdessart_current, I found this workaround works for me until they patch it in all versions:

    Code (CSharp):
    1. Texture2D tex = ScreenCapture.CaptureScreenshotAsTexture();
    2. ImageConversion.LoadImage(tex, ImageConversion.EncodeToPNG(tex)); //BUG workaround
    or

    Code (CSharp):
    1. Texture2D tex = ScreenCapture.CaptureScreenshotAsTexture();
    2. tex.LoadImage(tex.EncodeToPNG()); //BUG workaround
    There may be a more efficient way but this is sufficient for me for now.
     
    Last edited: Apr 24, 2018
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    3,020
    This is fixed in 2017.4.1f1
     
  9. juanitogan

    juanitogan

    Joined:
    Aug 18, 2017
    Posts:
    14
    Thank you. Apparently, I missed finding that update when looking last night. Tunnel vision I guess. I don't see a reference to any screenshot fixes in that version but I'll give it a try when the team is ready to move on it.
     
    karl_jones likes this.
  10. juanitogan

    juanitogan

    Joined:
    Aug 18, 2017
    Posts:
    14
    Yep, 2017.4.1f1 fixes the primary issue... but a lingering issue I didn't mention yesterday still exists. Something is amiss in color conversions. I'm building a 2D game primarily with meshes and the meshes with alpha colors don't screenshoot correctly. The alpha grays, for example, (the shadows) blend as alpha orange in the UI and as alpha light blue when saved to PNG. So, in other words, the alpha colors are being filtered to thier full-red, half-green, no-blue, and full?-alpha parts in the UI; and to their no-red, half-green, full-blue, and full?-alpha parts in file saves. Or, something like that. I have alpha colors in a texture object as well but those come out fine. It is just the meshes that don't.
     
  11. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    3,020
    Sounds like a different bug. Could you please send a bug report?
     
  12. juanitogan

    juanitogan

    Joined:
    Aug 18, 2017
    Posts:
    14
    Yeah, I just threw it up here quickly in case it rung a bell with anyone. I'll send a bug report when I find time to isolate it and send it. Right now I'm in a hard push to add features and this is a minor annoyance, relatively. Thus, it could be a while before I get to this.
     
    karl_jones likes this.
  13. juanitogan

    juanitogan

    Joined:
    Aug 18, 2017
    Posts:
    14
    So, I finally went through the pain of taking a whole day to dig into my secondary issue here, isolate it into a test project, and report it. And, unsurprisingly, it looks like it isn't going to go anywhere. Case 1045705. The current status is that it is by design that my screenshots look different from what is on screen. ???

    So, for anyone else looking at this down the road, here's what is going on (and my earlier description wasn't quite accurate). The alpha shaders provided by SVG Importer (which has very very spotty support these days -- I fix bugs in it myself now) don't work correctly with CaptureScreenshotAsTexture() when an object has alpha values. What happens is that the screenshot steals the alpha values from that object with the SVG Importer shader and also applies them to the objects underneath it (or, probably, everything on the same pixel). In other words, Unity is processing the alpha values in a different way in screen shooting than it is during game play. But, this is by design in their own words so far. It seems wrong to me... but I'm no expert on this either. I haven't been using Unity long enough to claim any sort of expertise here. All I know is that what they call an accurate screenshot isn't what I would call an accurate screenshot and they are like, yeah, totally, we're good with that.

    Fortunately, for me, during all that testing I also took some time to find a workaround that works for me -- but it won't work for all use cases. I postprocess the screenshot texture to remove all alpha values before I display it on screen or save it to PNG. But, I don't need to do this in real time and don't have any other alpha areas that should really be alpha. If your requirements are otherwise, good luck.