Search Unity

RenderTexture usage for camera screenshot

Discussion in 'Editor & General Support' started by badescuga9, Sep 30, 2013.

  1. badescuga9

    badescuga9

    Joined:
    Dec 11, 2012
    Posts:
    37
    I'm having some problems using RenderTexture in the editor and on the device (ios). I am trying to get a screenshot from only a camera. In the editor, the image gets messed up and on the device i get a black texture and this error:

    OpenGLES error 0x0502 in /Users/.../GlesHelper.mm:262

    Here is the code:
    Code (csharp):
    1.  
    2.     yield return new WaitForEndOfFrame(); // this also captures gui, remove if you don't wanna capture gui
    3.        
    4.                 RenderTexture rt = new RenderTexture(Screen.width, Screen.height , 24 , RenderTextureFormat.ARGB32);
    5.             rt.useMipMap = false;
    6.         rt.antiAliasing =1;
    7.         RenderTexture.active = rt;
    8.         Camera.main.targetTexture = rt;
    9.        
    10.        
    11.         Texture2D shot = new Texture2D(Screen.width,Screen.height,TextureFormat.ARGB32,false);
    12.            
    13.    
    14.         Camera.main.Render();
    15.            
    16.         shot.ReadPixels(new Rect(0,0,Screen.width,Screen.height),0,0,false);
    17.                 shot.Apply();
    18.            
    19.         Camera.main.targetTexture = null;
    20.                 RenderTexture.active = null;
    21.         Destroy(rt);
    22.  
    23.                GameObject.Find("PlaneTest").renderer.material.mainTexture = shot;
    24.        
    25.    
    The code is called on a press event.
    What am i doing wrong?
     
  2. badescuga9

    badescuga9

    Joined:
    Dec 11, 2012
    Posts:
    37
    i've changed a bit of the code, but the problem was from the camera. The clear flag was set to Don't clear instead of Depth only