Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Screen capture on 2nd camera using RenderTexture not working on Android

Discussion in 'Editor & General Support' started by jarvinda, Oct 6, 2015.

  1. jarvinda

    jarvinda

    Joined:
    Jan 2, 2015
    Posts:
    4
    When trying to take a screen grab from a 2nd camera in my project, this image is then set on a texture in the UI.

    This works in the Unity Editor, but when building for Android and running on an Android device, the screen capture is always from the main camera, and never the 2nd camera. Has anyone come across this issue before?

    Thanks for your help in advance.


    Test project can be found at here:- https://www.dropbox.com/s/ttwc5hzu2634pm7/TestScreenGrab.zip?dl=0

    This is what it should look like, taken from Unity Editor :- https://www.dropbox.com/s/dfbqk2x89bix8nw/screenshot.PNG?dl=0


    Code used to take the screenshot :-
    Code (CSharp):
    1.  
    2. public void ButtonClicked()
    3. {
    4.     var screenshot = TakeScreenShot();
    5.     image.texture = screenshot;
    6. }
    7.  
    8. private Texture TakeScreenShot()
    9. {
    10.     var renderTexture = new RenderTexture(Screen.width, Screen.height, 24);
    11.     screenshotCamera.targetTexture = renderTexture;  
    12.     RenderTexture.active = renderTexture;
    13.     screenshotCamera.Render();
    14.  
    15.     // texturize rendering
    16.     float leftOffset = (Screen.width - Screen.height)/2f;
    17.     var screenshot = new Texture2D(Screen.height, Screen.height, TextureFormat.RGB24, false);
    18.     screenshot.ReadPixels(new Rect(leftOffset, 0, Screen.height, Screen.height), 0, 0);
    19.     screenshot.Apply();
    20.  
    21.     screenshotCamera.targetTexture = null;
    22.     RenderTexture.active = null;
    23.  
    24.     // return shot
    25.     return screenshot;
    26. }
    27.  
     
  2. jarvinda

    jarvinda

    Joined:
    Jan 2, 2015
    Posts:
    4
    Can anybody help with this please?
     
  3. jarvinda

    jarvinda

    Joined:
    Jan 2, 2015
    Posts:
    4
    Problem solved. For some reason Android doesn't like it if you specify the activeTexture before you call render on the camera.

    So change :-
    Code (CSharp):
    1. var renderTexture = new RenderTexture(Screen.width, Screen.height, 24);
    2.     screenshotCamera.targetTexture = renderTexture;
    3.     RenderTexture.active = renderTexture;
    4.     screenshotCamera.Render();
    5.  
    to :-
    Code (CSharp):
    1. var renderTexture = new RenderTexture(Screen.width, Screen.height, 24);
    2.     screenshotCamera.targetTexture = renderTexture;
    3.     screenshotCamera.Render();
    4.  
    5.     RenderTexture.active = renderTexture;
    6.  
     
    codebeans and Dantus like this.
  4. StefanGura

    StefanGura

    Joined:
    May 27, 2013
    Posts:
    1
    I just would like to thank U. It also solved my problem. Thanks
     
    codebeans likes this.
  5. codebeans

    codebeans

    Joined:
    Apr 7, 2014
    Posts:
    79
    Big thanks from me as well. Unity staff should look into this!