在unity中利用ReadRixels可以实现截屏,现在分析一下各个参数的作用。


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


public class ScreenShot : MonoBehaviour {


    public Texture2D texture;//建立一个纹理图保存图片,设为public方便查看
    public int a, b, c, d,e,f;//测试用变量,改变值并测试


// Use this for initialization
void Start () {
        texture = new Texture2D(1920, 1080, TextureFormat.RGB24, false);//初始化纹理

    }


    private void FixedUpdate()
    {
        if (Input.GetKeyUp(KeyCode.A)){
            texture.ReadPixels(new Rect(a, b, c, d), e, f);
            texture.Apply();//按下a键实现截屏,abcd为截取范围,ef为保存位置,最后apple一下保存
        }
    }

  }

rect这个对象是用来存储成对出现的参数,比如,一个矩形框的左上角坐标、宽度和高度。这里用它表示截取范围。

unity3d中用ReadRixels实现截屏

如图,ab为开始点坐标,cd表示截取大小(相对开始选择范围)。

ef则表示保存时左下角据图片左下角的坐标。(注意截取范围与保存都不要越界)

截图完成时会显示如下错误。

ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame.

这是由于屏幕没有渲染完就截屏造成的。

实际使用中应使用携程,调用yield return new WaitForEndOfFrame();//链接有关报错

相关文档



相关文章:

  • 2022-12-23
  • 2022-01-17
  • 2021-11-26
  • 2022-03-08
  • 2022-12-23
  • 2022-02-10
猜你喜欢
  • 2021-11-22
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
  • 2021-05-29
  • 2022-03-10
相关资源
相似解决方案