Unity 截屏:任意区域截屏、全屏截取

直接上码:

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

public class ppp : MonoBehaviour {
    public int capx = 0;
    public int capy = 0;
    public int capwidth = 700;
    public int capheight = 500;

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(KeyCode.A)) {
            //自定义截屏
            StartCoroutine (getScreenTexture());
            //unity 自带截屏,只能是截全屏
            Application.CaptureScreenshot ("shot.png");
        }
    }

    IEnumerator getScreenTexture() {
        yield return new WaitForEndOfFrame ();
        //需要正确设置好图片保存格式
        Texture2D t = new Texture2D (capwidth,capheight,TextureFormat.RGB24, true);
        //按照设定区域读取像素;注意是以左下角为原点读取
        t.ReadPixels (new Rect(capx, capy, capwidth, capheight), 0, 0, false);
        t.Apply ();
        //二进制转换
        byte[] byt = t.EncodeToPNG ();
        System.IO.File.WriteAllBytes (Application.dataPath + Time.time + ".png", byt);
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容