码迷,mamicode.com
首页 > 编程语言 > 详细

Unity之将Texture保存成png

时间:2017-11-06 11:15:48      阅读:598      评论:0      收藏:0      [点我收藏+]

标签:nbsp   pix   orm   article   path   end   code   initial   file   

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;

}

//将RenderTexture保存成一张png图片
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之将Texture保存成png

标签:nbsp   pix   orm   article   path   end   code   initial   file   

原文地址:http://www.cnblogs.com/alps/p/7791676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!