u3d保存RenderTexture为Png

来源:互联网 发布:今年的网络流行语 编辑:程序博客网 时间:2024/06/05 08:08
using UnityEngine;using System.Collections;using System.IO;public class SaveToPng : MonoBehaviour {    public RenderTexture inputTex;    public void save()    {        SaveRenderToPng(inputTex,"test","png");    }    static public Texture2D SaveRenderToPng(RenderTexture renderT,string folderName,string name)    {        int width = renderT.width;        int height = renderT.height;        Texture2D tex2d = new Texture2D(width, height, TextureFormat.ARGB32, false);        RenderTexture.active = renderT;        tex2d.ReadPixels(new Rect(0, 0, width, height), 0, 0);        tex2d.Apply();        byte[] b = tex2d.EncodeToPNG();        string sysPath = "c:/" + folderName;        if (!Directory.Exists(sysPath))            Directory.CreateDirectory(sysPath);        FileStream file = File.Open(sysPath + "/" +name +  GetTimeName() + ".png", FileMode.Create);        BinaryWriter writer = new BinaryWriter(file);        writer.Write(b);        file.Close();        return tex2d;    }    static public string GetTimeName()    {        return System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() +             System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() +             System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() +             System.DateTime.Now.Millisecond.ToString();    }}
原创粉丝点击