首页 > 编程语言 > 详细

在Unity3D中利用 RenderTexture 实现游戏内截图

时间:2021-01-12 16:39:36      阅读:241      评论:0      收藏:0      [点我收藏+]

https://my.oschina.net/u/4316056/blog/4002529

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class 截图 : MonoBehaviour {

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.S))
        {
            Debug.Log("Save");
            Save();
        }
    }

    private RenderTexture TargetTexture;

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        TargetTexture = source;
        Graphics.Blit(source, destination);
    }

    private void Save()
    {
        RenderTexture.active = TargetTexture;

        Texture2D png = new Texture2D(RenderTexture.active.width, RenderTexture.active.height, TextureFormat.ARGB32, true);
        png.ReadPixels(new Rect(0, 0, RenderTexture.active.width, RenderTexture.active.height), 0, 0);

        byte[] bytes = png.EncodeToPNG();
        string path = string.Format(@"D:\123.png");
        FileStream file = File.Open(path, FileMode.Create);
        BinaryWriter writer = new BinaryWriter(file);
        writer.Write(bytes);
        file.Close();
        writer.Close();

        Destroy(png);
        png = null;

        Debug.Log("Save Down");
    }
}

  

在Unity3D中利用 RenderTexture 实现游戏内截图

原文:https://www.cnblogs.com/nafio/p/14266957.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!