u3d保存RenderTexture为Png

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();
    }
}

你可能感兴趣的:(Unity3D实用技术笔记,Unity3d技术笔记)