Search code examples
c#unity-game-enginetextures

Saving Target Texture as an image


I have got a quad that has got a target texture set through Unity Editor. I would like to save the output visualization on the quad as an image. Are there any ways to save it as a quad? I have tried through texture2d but its just a black image that is being saved.


Solution

  • Try this

    private void SaveImage(Texture t, string path)
    {
        RenderTexture rt = new RenderTexture(t.width, t.height, 0);
        Graphics.Blit(t, rt);
        Texture2D t2d = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);
        t2d.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
        File.WriteAllBytes(path, t2d.EncodeToPNG());
    }
    

    Usage

    SaveImage(yourQuad.GetComponent<MeshRenderer>().material.mainTexture, "yourSavePath.png");