http://blog.csdn.net/bingheliefeng/article/details/51177505
Using Unityengine;
Using System.Collections;
Using System.IO;
public class Savetopng:monobehaviour {
Public Shader Outshader;
Public Texture Inputtex;
Use this for initialization
void Start () {
Saverendertexturetopng (inputtex,outshader,application.datapath+ "/temp", "ok.png");
}
public bool Saverendertexturetopng (Texture inputtex,shader outputshader, string contents, String pngname)
{
Rendertexture temp = rendertexture.gettemporary (inputtex.width, inputtex.height, 0, RENDERTEXTUREFORMAT.ARGB32);
Material mat = new Material (Outputshader);
Graphics.blit (Inputtex, temp, mat);
BOOL ret = saverendertexturetopng (temp, contents,pngname);
Rendertexture.releasetemporary (temp);
return ret;
}
Save Rendertexture as a PNG image
public bool Saverendertexturetopng (rendertexture rt,string Contents, string pngname)
{
Rendertexture prev = rendertexture.active;
rendertexture.active = RT;
texture2d png = new Texture2d (Rt.width, Rt.height, Textureformat.argb32, false);
Png. Readpixels (New Rect (0, 0, Rt.width, rt.height), 0, 0);
byte[] bytes = png. Encodetopng ();
if (! Directory.Exists (contents))
Directory.CreateDirectory (contents);
FileStream file = File.Open (contents + "/" + Pngname + ". png", FileMode.Create);
BinaryWriter writer = new BinaryWriter (file);
Writer. Write (bytes);
File. Close ();
Texture2d.destroyimmediate (PNG);
PNG = NULL;
rendertexture.active = prev;
return true;
}
}
Unity's texture saved as png