Search Unity

ScreenCapture.CaptureScreenshot black image android

Discussion in 'Daydream' started by ShadownHeart, Nov 9, 2017.

  1. ShadownHeart

    ShadownHeart

    Joined:
    Oct 8, 2015
    Posts:
    29
    Hi!
    I'm trying to simulate an in-game camera in a VR game. The concept is easy, after the users takes a picture he could then watch it later on an album. Currently, I'm using ScreenCapture.CaptureScreenshot for capturing the image and then loading the picture on a Texture2D image for the album. On UnityEditor(PC) works fine and the picture shows, but when I adapt it for mobile (android), the picture taken is all black. The picture itself is captured no problem, but is totally black.

    I've tryed some tips that I read online, like using an enumerator and WaitForEndOfFrame, but with no result.

    Code (CSharp):
    1. private IEnumerator takeScreenshootAnimation()
    2.     {
    3.         yield return new WaitForEndOfFrame();
    4.         ScreenCapture.CaptureScreenshot("Screenshot.png");
    Anyone nows or have had the same problem? Thanks for your time!
     
  2. reedny

    reedny

    Joined:
    Mar 4, 2017
    Posts:
    57
    I saw the same thing, and used a render texture to get a screen shot:

    Code (CSharp):
    1.  
    2.     private Texture2D eyedropperTexture;
    3.     private Rect eyeRect;
    4.     private bool firstGetPixel = true;
    5.     private RenderTexture eyedropperRenderTexture;
    6.  
    7.     public IEnumerator GetPixel(bool saveImage)
    8.     {
    9.         yield return new WaitForEndOfFrame();
    10.  
    11.         if (firstGetPixel)
    12.         {
    13.             firstGetPixel = false;
    14.  
    15.             eyedropperTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
    16.             eyeRect = new Rect(0, 0, Screen.width, Screen.height);
    17.  
    18.             eyedropperRenderTexture = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);
    19.             eyedropperRenderTexture.Create();
    20.  
    21.             eyedropperCamera.targetTexture = eyedropperRenderTexture;
    22.             eyedropperCamera.enabled = false;
    23.         }
    24.  
    25.         RenderTexture currentRT = RenderTexture.active;
    26.         try
    27.         {
    28.             RenderTexture.active = eyedropperCamera.targetTexture;
    29.             eyedropperCamera.backgroundColor = Camera.main.backgroundColor;
    30.             eyedropperCamera.transform.position = Camera.main.transform.position;
    31.             eyedropperCamera.transform.rotation = Camera.main.transform.rotation;
    32.             eyedropperCamera.Render();
    33.             eyedropperTexture.ReadPixels(new Rect(0, 0, eyedropperCamera.targetTexture.width, eyedropperCamera.targetTexture.height), 0, 0);
    34.         }
    35.         finally
    36.         {
    37.             RenderTexture.active = currentRT;
    38.         }
    39.     }
    40.  
     
  3. ShadownHeart

    ShadownHeart

    Joined:
    Oct 8, 2015
    Posts:
    29
    Thanks, that did the trick!