欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity | 部分区域*截图

程序员文章站 2022-03-21 23:47:01
...

一、固定区域截屏:

Unity三种截屏方法

二、*截图

using System.Collections;
using UnityEngine;

///挂在摄像机上
///如果要截图UI,需将Canvas的RenderMode设置为Screen Space-Camera,并将Render Camera设置为挂该脚本的摄像机

public class Capture : MonoBehaviour {
    public Color rColor = Color.green;//截图框颜色
    public Vector3 start = Vector3.zero;//开始位置
    public Vector3 end = Vector3.zero;//结束位置
    public Material rMat = null;//截图框材质,shader为"UI/Default"即可
    public bool drawFlag = false;//是否开始截图
    private Rect rect;//截图区域
    private Texture2D cutImage;//图片
    byte[] bytes;//图片byte
  
    void Update()
    {
        //按下鼠标左键开始截图
        if (Input.GetMouseButtonDown(0) && SecondPageManager._instance. ToCapture)
        {
            SecondPageManager._instance.CapturePanel.SetActive(false);
            drawFlag = true;
            start = Input.mousePosition;
        }
        //抬起鼠标左键结束截图
        if (Input.GetMouseButtonUp(0) && SecondPageManager._instance.ToCapture && drawFlag)
        {
            SecondPageManager._instance.ToCapture = false;
            drawFlag = false;
            StartCoroutine(CutImage());
        }
        //点击鼠标右键取消截图
        if (Input.GetMouseButtonDown(1) && SecondPageManager._instance.ToCapture)
        {
            SecondPageManager._instance.CapturePanel.transform.parent.gameObject.SetActive(false);
            SecondPageManager._instance.CapturePanel.SetActive(false);
            SecondPageManager._instance.ToCapture = false;
            drawFlag = false;
        }
    }
    //绘制框选  
    void OnPostRender()
    { 
        if (drawFlag)
        {
            end = Input.mousePosition;//鼠标当前位置  
            GL.PushMatrix();//保存摄像机变换矩阵  
            if (!rMat)
                return;
            rMat.SetPass(0);
            GL.LoadPixelMatrix();//设置用屏幕坐标绘图  
            GL.Begin(GL.QUADS);
            GL.Color(new Color(rColor.r, rColor.g, rColor.b, 0.2f));//设置颜色和透明度,方框内部透明  
            GL.Vertex3(start.x, start.y, 0);
            GL.Vertex3(end.x, start.y, 0);
            GL.Vertex3(end.x, end.y, 0);
            GL.Vertex3(start.x, end.y, 0);
            GL.End();
            GL.Begin(GL.LINES);
            GL.Color(rColor);//设置方框的边框颜色 边框不透明  
            GL.Vertex3(start.x, start.y, 0);
            GL.Vertex3(end.x, start.y, 0);
            GL.Vertex3(end.x, start.y, 0);
            GL.Vertex3(end.x, end.y, 0);
            GL.Vertex3(end.x, end.y, 0);
            GL.Vertex3(start.x, end.y, 0);
            GL.Vertex3(start.x, end.y, 0);
            GL.Vertex3(start.x, start.y, 0);
            GL.End();
            GL.PopMatrix();//恢复摄像机投影矩阵  
        }
    }
    IEnumerator CutImage()
    {

        string date = System.DateTime.Now.ToString("yyyyMMddHHmmss");
        //图片大小    
        if (end.x > start.x&& start.y > end.y)
        {
            cutImage = new Texture2D((int)(end.x - start.x), (int)(start.y - end.y), TextureFormat.RGB24, true);
            //坐标左下角为(0,0)点
            rect = new Rect((int)start.x,
                Screen.height - (int)(Screen.height - end.y), 
                (int)(end.x - start.x),
                (int)(start.y - end.y));
            yield return new WaitForEndOfFrame();
            cutImage.ReadPixels(rect, 0, 0, false);
            cutImage.Apply();
            yield return cutImage;
            bytes = cutImage.EncodeToPNG();
            //保存截图    
            //File.WriteAllBytes("D:/CutImage" + date + ".png", bytes);
            //公式识别
            StartCoroutine(FormulaRecognize._instance.OCRIdentify(bytes));
        }
        else
        {
            StartCoroutine(FormulaRecognize._instance.OCRIdentify(null));
            Debug.Log("请按照左上角到右下角的方式截图"); 
        }
    }
}

Unity | 部分区域*截图

                                    Unity | 部分区域*截图    Unity | 部分区域*截图

相关标签: Unity