Search Unity

Bug tex.ReadPixels only work on button!?

Discussion in 'General Graphics' started by Ikaro88, Oct 22, 2022.

  1. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    300
    Hi, I have to transform the camera render to a texture2d.

    this is the method I use:


    Code (CSharp):
    1.   public static Texture2D RenderTextureToTexture(RenderTexture rTex)
    2.     {
    3.         // Remember currently active render texture
    4.         RenderTexture currentActiveRT = RenderTexture.active;
    5.         //backup
    6.         // ReadPixels looks at the active RenderTexture.
    7.         RenderTexture.active = rTex;
    8.  
    9.         // Texture2D tex = new Texture2D(rTex.width, rTex.height, TextureFormat.RGB24, false);
    10.         Texture2D tex = new Texture2D(rTex.width, rTex.height, TextureFormat.RGB24, false);
    11.      
    12.         tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
    13.         tex.Apply();
    14.  
    15.         RenderTexture.active = currentActiveRT;
    16.         return tex;
    17.     }
    On editor all work correctly.

    On my quest 2, if I use this function from 3 separate a buttons it work correctly
    If I call it from a Corutine the returned texture is wrong....

    I also tried waiting more than 30 seconds between each call but I still have the problem...

    Any help?
     
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    675
  3. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    300
    I also tried waiting more than 30 seconds, and I also tried WaitForEndOfFrame as well...nothing seems to fix it