u3d保存RenderTexture为Png

2023-08-24 13:42:25 浏览数 (3)

代码语言:javascript复制
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();
    }
}

1 人点赞