title: How to access raw data from RenderTexture in Unity
body: <h2>Short Version of Problem</h2>
  
  <p>I am trying to access the contents of a RenderTexture in Unity which I have been drawing with an own Material using Graphics.Blit.</p>
  
  <pre><code>Graphics.Blit (null, renderTexture, material);
  </code></pre>
  
  <p>My material converts some yuv image to rgb successfully, which I have tested by assigning it to the texture of an UI element. The result is the correct RGB image visible on the screen.</p>
  
  <p>However, I also need the raw data for a QR code scanner. I am doing this like I would access it from a camera as explained <a href="http://answers.unity3d.com/questions/9969/convert-a-rendertexture-to-a-texture2d.html" rel="nofollow noreferrer">here</a>. In a comment down there, it was mentioned that the extraction is also possible from a RenderTexture that was filled with Graphics.Blit. But when I am trying to that, my texture only contains the value 205 everywhere. This is the code I am using in the Update function, directly after the Graphics.Blit call:</p>
  
  <pre><code>RenderTexture.active = renderTexture;
  texture.ReadPixels (new Rect (0, 0, width, height), 0, 0);
  texture.Apply ();
  RenderTexture.active = null;
  </code></pre>
  
  <p>When assigning this texture to the same UI element, it is gray and slightly transparent. When viewing the image values, they are all 205.</p>
  
  <p>Why is the possible? May there be problems with the formats that the RenderTexture and Texture2D I am trying to fill?</p>
  
  <h2>Complete Code</h2>
  
  <p>In the following I add the whole code I am using. The names of the variables slightly differ to the ones I used above but they do essentially the same:</p>
  
  <pre><code>/** 
   * This class continously converts the y and uv textures in 
   * YUV color space to a RGB texture, which can be used somewhere else
   */
  public class YUV2RGBConverter : MonoBehaviour {
      public Material yuv2rgbMat;
  
      // Input textures, set these when they are available
      [HideInInspector]
      public Texture2D yTex;
      [HideInInspector]
      public Texture2D uvTex;
  
      // Output, the converted textures
      [HideInInspector]
      public RenderTexture rgbRenderTex;
      [HideInInspector]
      public Texture2D rgbTex;
      [HideInInspector]
      public Color32[] rawRgbData;
  
      /// Describes how often per second the image should be transferred to the CPU
      public float GPUTransferRate = 1.0f;
      private float timeSinceLastGPUTransfer = 0.0f;
  
      private int width;
      private int height;
  
      /**
       * Initializes the used textures 
       */
      void Start () {
          updateSize (width, height);
      }
  
      /**
       * Depending on the sizes of the texture, creating the needed textures for this class 
       */
      public void updateSize(int width, int height)
      {
          // Generate the input textures
          yTex = new Texture2D(width / 4, height, TextureFormat.RGBA32, false);
          uvTex = new Texture2D ((width / 2) * 2 / 4, height / 2, TextureFormat.RGBA32, false);
  
          // Generate the output texture
          rgbRenderTex = new RenderTexture(width, height, 0);
          rgbRenderTex.antiAliasing = 0;
          rgbTex = new Texture2D (width, height, TextureFormat.RGBA32, false);
  
          // Set to shader
          yuv2rgbMat.SetFloat("_TexWidth", width);
          yuv2rgbMat.SetFloat("_TexHeight", height);
      }
  
      /**
       * Sets the y and uv textures to some dummy data
       */
      public void fillYUWithDummyData()
      {
          // Set the y tex everywhere to time rest
          float colorValue = (float)Time.time - (float)((int)Time.time); 
          for (int y = 0; y &lt; yTex.height; y++) {
              for (int x = 0; x &lt; yTex.width; x++) {
                  Color yColor = new Color (colorValue, colorValue, colorValue, colorValue);
                  yTex.SetPixel (x, y, yColor);
              }
          }
          yTex.Apply ();
  
          // Set the uv tex colors 
          for (int y = 0; y &lt; uvTex.height; y++) {
              for (int x = 0; x &lt; uvTex.width; x++) {
                  int firstXCoord = 2 * x;
                  int secondXCoord = 2 * x + 1;
                  int yCoord = y;
  
                  float firstXRatio = (float)firstXCoord / (2.0f * (float)uvTex.width);
                  float secondXRatio = (float)secondXCoord / (2.0f * (float)uvTex.width);
                  float yRatio = (float)y / (float)uvTex.height;
  
                  Color uvColor = new Color (firstXRatio, yRatio, secondXRatio, yRatio);
                  uvTex.SetPixel (x, y, uvColor);
              }
          }
          uvTex.Apply ();
      }
  
      /**
       * Continuously convert y and uv texture to rgb texture with custom yuv2rgb shader
       */
      void Update () {
          // Draw to it with the yuv2rgb shader
          yuv2rgbMat.SetTexture ("_YTex", yTex);
          yuv2rgbMat.SetTexture ("_UTex", uvTex);
          Graphics.Blit (null, rgbRenderTex, yuv2rgbMat);
  
          // Only scan once per second
          if (timeSinceLastGPUTransfer &gt; 1 / GPUTransferRate) {
              timeSinceLastGPUTransfer = 0;
  
              // Fetch its pixels and set it to rgb texture
              RenderTexture.active = rgbRenderTex;
              rgbTex.ReadPixels (new Rect (0, 0, width, height), 0, 0);
              rgbTex.Apply ();
              RenderTexture.active = null;
  
              rawRgbData = rgbTex.GetPixels32 ();
          } else {
              timeSinceLastGPUTransfer += Time.deltaTime;
          }
      }
  }
  </code></pre>