前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >u3d保存RenderTexture为Png

u3d保存RenderTexture为Png

作者头像
立羽
发布2023-08-24 13:42:25
2350
发布2023-08-24 13:42:25
举报
文章被收录于专栏:Unity3d程序开发Unity3d程序开发
代码语言: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();
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-06-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档