美文网首页
Unity 截屏:任意区域截屏、全屏截取

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

作者: 木子才 | 来源:发表于2017-05-15 09:10 被阅读0次

直接上码:

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);
    }

}

相关文章

网友评论

      本文标题:Unity 截屏:任意区域截屏、全屏截取

      本文链接:https://www.haomeiwen.com/subject/rywcxxtx.html