Unity's texture saved as png

Source: Internet
Author: User
Tags prev

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.