广州市手机网站建设怎么样,用dw设计网站模板下载地址,建设个人网站详细点,网站创建设计SEO优化象客提示#xff1a;文章有错误的地方#xff0c;还望诸位大神不吝指教#xff01; 文章目录 前言一、第一种方法#xff08;1#xff09;简介GL#xff08;2#xff09; GL 用法#xff1a;#xff08;3#xff09;具体代码 二、第二种方法第一步#xff1a;第二步第三… 提示文章有错误的地方还望诸位大神不吝指教 文章目录 前言一、第一种方法1简介GL2 GL 用法3具体代码 二、第二种方法第一步第二步第三步 代码 总结 前言
unity版本2019.3.7f1
本文提供两种截图方式都是亲测可用。 第一种使用GL画线来实现 第二种使用Image来实现 总的逻辑都是一样的。 下面是两种方式的效果图
提示以下是本篇文章正文内容下面案例可供参考
一、第一种方法
使用GL来画一个框作为截图区域。
1简介GL
Graphics Library简称 GL包含一系列类似 OpenGL 的 Immediate 模式的渲染指令比 Graphic.DrawMesh() 更高效。GL 是立即执行的如果在Update() 方法里调用它们将在相机渲染前执行相机渲染前会清空屏幕GL 渲染效果将无法看到。
2 GL 用法
在相机上挂脚本并在 OnPostRender() 方法里执行MonoBehaviour的生命周期。GL 渲染的图像不需要 GameObject 承载在 Hierarchy 窗口不会生成 GameObject 对象。 另外需要给一个Material 作为材质
3具体代码
记得挂载在相机上并且给一个Material 。
/**********************************************************************文件信息文件名(File Name): SelectScreenshot.cs作者(Author): 野区捕龙为宠创建时间(CreateTime): #CREATETIME#**********************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
using System.IO;namespace Twq
{public class SelectScreenshot : MonoBehaviour{private Color rColor Color.green;private Vector3 start Vector3.zero;private Vector3 end Vector3.zero;public Material rMat;private bool drawFlag false;private Rect rect;private Texture2D cutImage;// Use this for initialization void Start(){}// Update is called once per frame void Update(){if (Input.GetMouseButtonDown(0)){drawFlag true;//如果鼠标左键按下 设置开始画线标志 start Input.mousePosition;//记录按下位置 end Input.mousePosition;//鼠标当前位置 }else if (Input.GetMouseButtonUp(0)){drawFlag false;//如果鼠标左键放开 结束画线 }if (Input.GetKeyDown(KeyCode.M)){StartCoroutine(CutImage());}}//绘制框选 void OnPostRender(){//画线这种操作推荐在OnPostRender里进行 而不是直接放在Update所以需要标志来开启 if (drawFlag){end Input.mousePosition;//鼠标当前位置 }GL.PushMatrix();//保存摄像机变换矩阵 if (!rMat)return;rMat.SetPass(0);GL.LoadPixelMatrix();//设置用屏幕坐标绘图 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);//图片大小 cutImage new Texture2D((int)(end.x - start.x), (int)(start.y - end.y), TextureFormat.RGB24, true);//坐标左下角为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, true);cutImage.Apply();yield return cutImage;byte[] byt cutImage.EncodeToPNG();//保存截图 File.WriteAllBytes(Application.streamingAssetsPath /ImageName date .png, byt);}}
}
二、第二种方法
原理都差不多第二种扩展性更好一些。 需要创建4个UI来充当线框。并设置好Anchors。
第一步
创建一个父节点以及子节点。如下图
第二步
分别创建4个UI充当4边线框并设置每个边其Anchors。如下图 第三步 代码
using System.Collections;
using System.Collections.Generic;
//using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;public class MarkerPointSet : MonoBehaviour
{private Vector3 m_Point;// private PointBase m_PointBase;private ListGameObject m_Images new ListGameObject();ListTexture2D m_AddListTexture new ListTexture2D();//private BaoGaoPoint m_BaoGaoPoint;private Texture2D m_screenShot;//画框截图public GameObject m_LineNode;public GameObject m_LeftLine;public GameObject m_RightLine;public GameObject m_DownLine;public GameObject m_UpLine;private Vector3 m_StartLine Vector3.zero;private Vector3 m_EndLien Vector3.zero;private bool m_IsScreens false;private bool m_IsMouseEnd false;void Start(){m_IsScreens true;}private void Update(){if (m_IsScreens true){m_EndLien Input.mousePosition;if (Input.GetMouseButtonDown(0)){m_StartLine Input.mousePosition;m_LineNode.gameObject.SetActive(true);m_IsMouseEnd true;}else if(Input.GetMouseButtonUp(0)){if (m_IsMouseEnd true){m_EndLien Input.mousePosition;m_IsScreens false;m_IsMouseEnd false;//m_Buttons.SetActive(true);//m_Buttons.transform.position Input.mousePosition;int _x (int)m_StartLine.x;int _y (int)m_StartLine.y;int _w (int) (m_StartLine.x - m_EndLien.x);int _h (int)(m_StartLine.y - m_EndLien.y);if (_w 0){_w Mathf.Abs(_w);}else{_x - _w;}if (_h 0){_h Mathf.Abs(_h);}else{_y - _h;}StartCoroutine(CaptureScreen(new Rect(_x,_y,_w,_h)));}}if (m_IsMouseEnd true){UpdateLine();}}}private void UpdateLine(){m_LeftLine.transform.position m_StartLine;m_DownLine.transform.position m_StartLine;m_RightLine.transform.position m_EndLien;m_UpLine.transform.position m_EndLien;float _x m_StartLine.x - m_EndLien.x;float _y m_StartLine.y - m_EndLien.y;if (_x 0){RectTransform _dtrans m_DownLine.GetComponentRectTransform();_dtrans.sizeDelta new Vector2(_x,2f);RectTransform _utrans m_UpLine.GetComponentRectTransform();_utrans.sizeDelta new Vector2(_x, 2f);_dtrans.localScale new Vector3(-1f,1f,1f);_utrans.localScale new Vector3(-1f, 1f, 1f);} else{_x Mathf.Abs(_x);RectTransform _dtrans m_DownLine.GetComponentRectTransform();_dtrans.sizeDelta new Vector2(_x, 2f);RectTransform _utrans m_UpLine.GetComponentRectTransform();_utrans.sizeDelta new Vector2(_x, 2f);_dtrans.localScale new Vector3(1f, 1f, 1f);_utrans.localScale new Vector3(1f, 1f, 1f);}if (_y 0){RectTransform _dtrans m_LeftLine.GetComponentRectTransform();_dtrans.sizeDelta new Vector2(2f, _y);RectTransform _utrans m_RightLine.GetComponentRectTransform();_utrans.sizeDelta new Vector2(2f, _y);_dtrans.localScale new Vector3(1f, -1f, 1f);_utrans.localScale new Vector3(1f, -1f, 1f);}else{_y Mathf.Abs(_y);RectTransform _dtrans m_LeftLine.GetComponentRectTransform();_dtrans.sizeDelta new Vector2(2f, _y);RectTransform _utrans m_RightLine.GetComponentRectTransform();_utrans.sizeDelta new Vector2(2f, _y);_dtrans.localScale new Vector3(1f, 1f, 1f);_utrans.localScale new Vector3(1f, 1f, 1f);}}private IEnumerator CaptureScreen(Rect rect){yield return new WaitForEndOfFrame();//不显示UI截图RenderTexture _rt new RenderTexture(1920,1080,0);Camera.main.targetTexture _rt;//传到主摄像机上Camera.main.Render();//渲染RenderTexture.active _rt;m_screenShot new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);m_screenShot.ReadPixels(rect, 0, 0);m_screenShot.Apply();Camera.main.targetTexture null;RenderTexture.active null;Destroy(_rt);//m_Image.gameObject.SetActive(true);//m_Image.rectTransform.sizeDelta new Vector2(m_screenShot.width, m_screenShot.height);//图片设置//m_Image.sprite Sprite.Create(m_screenShot, new Rect(0, 0, m_screenShot.width, m_screenShot.height), Vector2.zero);yield break;}}
项目链接: link
总结
好记性不如烂笔头