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();
}
}
u3d保存RenderTexture为Png
最新推荐文章于 2025-11-14 10:31:53 发布
本文介绍了一个Unity脚本,该脚本能将RenderTexture渲染的内容保存为PNG格式的图片文件,并支持自动创建目录及时间戳命名。此方法适用于游戏开发中截图功能的实现。
2万+

被折叠的 条评论
为什么被折叠?



