Unity截圖成png格式保存到本地的三種方式

Unity截圖成png格式保存到本地的三種方式

  • 第一種截圖方式,直接用unity自帶截圖函數ScreenCapture.CaptureScreenshot,全屏截圖,不能改變圖像尺寸.
  • 第二種截圖方式,將屏幕圖像存儲到一個texture2D中去,可以設定圖片尺寸
  • 第三種截圖方式,將rendertexture存儲到一個texture2D中去,可以設定圖片尺寸
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class save : MonoBehaviour
{
    //保存圖像信息,時間戳+像素RGB
    private  struct ImageData
    {
        public long timestamp;
        public byte[] data;
    }
    //tooltip在面板中變量註釋提示
    [Tooltip("截圖寬度px")]
    public int width;
    [Tooltip("截圖高度py")]
    public int height;
    [Tooltip("截圖存儲路徑")]
    public string path;
    public Camera cam;
    public bool capture;
    Texture2D mtexture;
    RenderTexture rt;
    Rect ret;
    void Start()
    {
        //如果沒有找到camera,那麼獲得camera組件,該腳本要掛載到你的camera上
        if (cam == null)
        {
            cam = GetComponent<Camera>();
        }
    }
    //捕獲屏幕與保存不要放在主線程中,放在協程或者單獨開一個線程
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.C))
        {
            if (!capture)
            {
                cam.enabled = true;
                capture = true;
                StartCoroutine(CaptureAndSave03());
                Debug.Log("REC!");
            }
            else
            {
                cam.enabled = false;
                capture = false;
                StopCoroutine(CaptureAndSave03());
                Debug.Log("REC STOP!");
            }
        }
    }
    //第一種截圖方式,直接用unity自帶截圖函數,全屏截圖,不能改變圖像尺寸
    public IEnumerator CaptureAndSave01()
    {
        while(capture)
        {
        ImageData pack;
        pack.timestamp = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
        ScreenCapture.CaptureScreenshot(pack.timestamp + ".png");
        yield return new WaitForSeconds(1);
        }
    }
    //第二種截圖方式,將屏幕 圖像存儲到一個texture2D中去,可以設定圖片尺寸
    public IEnumerator CaptureAndSave02()
    {
        while(capture)
        {
            //等待屏幕渲染結束後獲取屏幕像素信息
            yield return new WaitForEndOfFrame();
            //創建一個texture2D對象
            mtexture = new Texture2D(width, height, TextureFormat.RGB24, false);
            ret.width = width;
            ret.height = height;
            //讀取屏幕像素信息存儲爲紋理數據
            mtexture.ReadPixels(ret, 0, 0);
            mtexture.Apply();
            //將圖片信息編碼爲字節信息 
            ImageData pack;
            pack.data = mtexture.EncodeToPNG();
            //保存圖片到 你的路徑
            pack.timestamp = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            FileStream file = File.Create(pack.timestamp + ".png");
            file.Write(pack.data, 0, pack.data.Length);
            file.Close();
        }
    }
    //通過camera截取
    public IEnumerator CaptureAndSave03()
    {
        while(capture)
        {
        yield return new WaitForEndOfFrame();
        //創建一個rendertexture
        rt = new RenderTexture(width, height, 0);
        //將rt設置爲相機的渲染目標
        cam.targetTexture = rt;
        //開始渲染
        cam.Render();
        //激活渲染貼紙讀取信息
        RenderTexture.active = rt;
        mtexture = new Texture2D(width, height, TextureFormat.RGB24, false);
        ret.width = width;
        ret.height = height;
        //讀取屏幕像素信息存儲爲紋理數據
        mtexture.ReadPixels(ret, 0, 0);
        mtexture.Apply();
        //釋放相機,銷燬渲染貼紙
        cam.targetTexture = null;
        RenderTexture.active = null;
        GameObject.Destroy(mtexture);
        //將圖片信息編碼爲字節信息 
        ImageData pack;
        pack.data = mtexture.EncodeToPNG();
        //保存圖片到 你的路徑
        pack.timestamp = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
        FileStream file = File.Create(pack.timestamp + ".png");
        file.Write(pack.data, 0, pack.data.Length);
        file.Close();
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章