u3d保存RenderTexture为Png

xiaoxiao2021-02-28  80

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(); } }
转载请注明原文地址: https://www.6miu.com/read-27068.html

最新回复(0)