截图并保存

未运行情况

#if UNITY_EDITOR
        string deskTopPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
        RenderTexture.active = captureCamera.targetTexture;
        Texture2D tex = new Texture2D(captureCamera.targetTexture.width, captureCamera.targetTexture.height, TextureFormat.ARGB32, false);
        tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
        RenderTexture.active = null;
        byte[] bytes = tex.EncodeToJPG();
        System.IO.File.WriteAllBytes(
       deskTopPath + "\\绘画截图\\" + System.DateTime.Now.Ticks + ".jpg", bytes);
#endif

运行情况下

using System.IO;
using UnityEngine;

public class Capture : MonoBehaviour
{
    public float picWidth = 1920f;
    public float picHeight = 1080f;
    //文件名(时间戳)
    string fileName;
    //文件路径
    string filePath;
    void Start()
    {
        filePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)
            + "\\全景图\\";//桌面文件夹
        if (Directory.Exists(filePath) == false)//如果没有找到这个文件夹就创建一个
        {
            Directory.CreateDirectory(filePath);
        }
        fileName = System.DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");

       
        
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            CameraCapture(Camera.main);
        }
    }

    /// <summary>
    /// 截取图片
    /// </summary>
    /// <param name="m_Camera"></param>
    /// <param name="filename"></param>
    /// 
    Texture2D t2d;
    void CameraCapture(Camera m_Camera)
    {
        RenderTexture rt = new RenderTexture((int)picWidth, (int)picHeight, 16);
        m_Camera.targetTexture = rt;
        m_Camera.Render();
        RenderTexture.active = rt;
        Texture2D t = new Texture2D((int)picWidth, (int)picHeight);
        t.ReadPixels(new Rect(0, 0, t.width, t.height), 0, 0);
        t.Apply();
        t2d = t;
        //数据保存路径
        string path = filePath + fileName + ".png";
        System.IO.File.WriteAllBytes(path, t.EncodeToPNG());
        m_Camera.targetTexture = null;
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝天下的吻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值