unity 3d导出rendertexture

[csharp] view plain copy
[ContextMenu("Save png")]  
    private void SaveTextureToFile()  
    {  
        if (OutputTexture != null)  
        {  
            RenderTexture prev = RenderTexture.active;  
            RenderTexture.active = target;  
  
            Texture2D png = new Texture2D(OutputTexture.width, OutputTexture.height, TextureFormat.ARGB32, false);  
            png.ReadPixels(new Rect(0, 0, OutputTexture.width, OutputTexture.height), 0, 0);  
            byte[] bytes =  png.EncodeToPNG();  
            string path = string.Format("Dump/raw {0}.png", Random.Range(0, 65536).ToString("X"));   
            FileStream file = File.Open(path, FileMode.Create);  
              
            BinaryWriter writer = new BinaryWriter(file);  
            writer.Write(bytes);  
            file.Close();  
  
            Texture2D.Destroy(png);  
            png = null;  
  
            RenderTexture.active = prev;  
        }  
  
    }

你可能感兴趣的:(Unity)