Unity之将Texture保存成png
Posted alps_01
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity之将Texture保存成png相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章
游戏开发创新Unity狗屁不通文章生成器阐述点赞的意义,可生成文字长图保存到本地(Unity | 附源码 | Text转Texture长图 | 详细教程)