Unity | 部分区域自由截图

匿名 (未验证) 提交于 2019-12-02 23:36: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("请按照左上角到右下角的方式截图");          }     } }

文章来源: https://blog.csdn.net/weixin_39766005/article/details/90641950
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!