Unity實(shí)現(xiàn)全屏截圖以及QQ截圖
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)全屏截圖、Unity實(shí)現(xiàn)QQ截圖,供大家參考,具體內(nèi)容如下
全屏截圖:要實(shí)現(xiàn)的是點(diǎn)擊鼠標(biāo)左鍵,就實(shí)現(xiàn)截圖,并且將所截圖片保存到本地Assets目錄下的StreamingAssets文件夾下面。
代碼如下:
using UnityEngine;
using System.Collections;
public class TakeScreenShot : MonoBehaviour {
void Update () {
//點(diǎn)擊鼠標(biāo)左鍵
if (Input.GetMouseButtonDown (0)) {
//開啟協(xié)程方法
StartCoroutine (MyCaptureScreen ());
}
}
//我的截屏方法
IEnumerator MyCaptureScreen(){
//等待所有的攝像機(jī)和GUI被渲染完成。
yield return new WaitForEndOfFrame ();
//創(chuàng)建一個(gè)空紋理(圖片大小為屏幕的寬高)
Texture2D tex = new Texture2D (Screen.width,Screen.height);
//只能在幀渲染完畢之后調(diào)用(從屏幕左下角開始繪制,繪制大小為屏幕的寬高,寬高的偏移量都為0)
tex.ReadPixels (new Rect (0,0,Screen.width,Screen.height),0,0);
//圖片應(yīng)用(此時(shí)圖片已經(jīng)繪制完成)
tex.Apply ();
//將圖片裝換成jpg的二進(jìn)制格式,保存在byte數(shù)組中(計(jì)算機(jī)是以二進(jìn)制的方式存儲數(shù)據(jù))
byte[] result = tex.EncodeToJPG ();
//文件保存,創(chuàng)建一個(gè)新文件,在其中寫入指定的字節(jié)數(shù)組(要寫入的文件的路徑,要寫入文件的字節(jié)。)
System.IO.File.WriteAllBytes (Application.streamingAssetsPath+"/1.JPG",result);
}
}
之后思考一下,如何像qq截圖那樣,繪制一個(gè)截屏的矩形框只截取自己想要的部分呢?
using UnityEngine;
using System.Collections;
using System.IO;//引入IO流
public class NewBehaviourScript : MonoBehaviour {
//定義一個(gè)存儲截屏圖片的路徑
string filePath;
void Start () {
//圖片存儲在StreamingAssets文件夾內(nèi)。
filePath = Application.streamingAssetsPath + "/1.png";
}
Rect rect;
//截屏開始的位置
Vector3 s_pos;
//截屏結(jié)束的位置
Vector3 e_pos;
//是否繪制
bool isDraw;
void Update()
{
//按下鼠標(biāo)左鍵時(shí),記錄當(dāng)前鼠標(biāo)的位置為開始截屏?xí)r的位置
if(Input.GetMouseButtonDown(0))
{
s_pos = Input.mousePosition;
}
//鼠標(biāo)處于按下狀態(tài)時(shí)
if(Input.GetMouseButton(0))
{
e_pos = Input.mousePosition;
//可以開始繪制
isDraw = true;
}
//抬起鼠標(biāo)左鍵時(shí),記錄當(dāng)前鼠標(biāo)的位置為結(jié)束截屏?xí)r的位置
if(Input.GetMouseButtonUp(0))
{
//結(jié)束繪制
isDraw = false;
e_pos = Input.mousePosition;
//獲取到截屏框起始點(diǎn)的位置,和寬高。
rect = new Rect(Mathf.Min(s_pos.x,e_pos.x),Mathf.Min(s_pos.y,e_pos.y),Mathf.Abs(s_pos.x-e_pos.x),Mathf.Abs(s_pos.y - e_pos.y));
//開啟繪制的協(xié)程方法
StartCoroutine(Capsture(filePath, rect));
}
}
IEnumerator Capsture(string filePath, Rect rect)
{
yield return new WaitForEndOfFrame();
//創(chuàng)建紋理(紋理貼圖的大小和截屏的大小相同)
Texture2D tex = new Texture2D((int)rect.width, (int)rect.height);
//讀取像素點(diǎn)
tex.ReadPixels(rect, 0, 0) ;
//將像素點(diǎn)應(yīng)用到紋理上,繪制圖片
tex.Apply();
//將圖片裝換成jpg的二進(jìn)制格式,保存在byte數(shù)組中(計(jì)算機(jī)是以二進(jìn)制的方式存儲數(shù)據(jù))
byte[] result =tex.EncodeToPNG();
//文件夾(如果StreamAssets文件夾不存在,在Assets文件下創(chuàng)建該文件夾)
if(!Directory.Exists(Application.streamingAssetsPath))
Directory.CreateDirectory(Application.streamingAssetsPath);
//將截屏圖片存儲到本地
File.WriteAllBytes(filePath, result);
}
//在這里要用GL實(shí)現(xiàn)繪制截屏的矩形框
//1.GL的回調(diào)函數(shù)
//2.定義一個(gè)材質(zhì)Material
public Material lineMaterial;
void OnPostRender() {
if(!isDraw) return;
print (s_pos);
Vector3 sPos = Camera.main.ScreenToWorldPoint(s_pos + new Vector3(0, 0, 10));
Vector3 ePos = Camera.main.ScreenToWorldPoint(e_pos + new Vector3(0, 0, 10));
print(string.Format("GL.....{0}, {1}", sPos, ePos));
// Set your materials Done
GL.PushMatrix();
// yourMaterial.SetPass( );
lineMaterial.SetPass(0);//告訴GL使用該材質(zhì)繪制
// Draw your stuff
//始終在最前面繪制
GL.invertCulling = true;
GL.Begin(GL.LINES);//開始繪制
//GL.Vertex(sPos);
//GL.Vertex(ePos);
//如果想要繪制,矩形,將下面代碼啟動
GL.Vertex(sPos);
GL.Vertex(new Vector3(ePos.x, sPos.y, 0));
GL.Vertex(new Vector3(ePos.x, sPos.y, 0));
GL.Vertex(ePos);
GL.Vertex(ePos);
GL.Vertex(new Vector3(sPos.x, ePos.y, 0));
GL.Vertex(new Vector3(sPos.x, ePos.y, 0));
GL.Vertex(sPos);
GL.End();//結(jié)束繪制
GL.PopMatrix();
}
}
在腳本組件中拖入材質(zhì)球

此圖片黑色的矩形框就是通過GL繪制出來的(通過記錄點(diǎn)的位置來依次繪制出線)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入解析C#設(shè)計(jì)模式編程中對建造者模式的運(yùn)用
這篇文章主要介紹了C#設(shè)計(jì)模式編程中對建造者模式的運(yùn)用,文中還介紹了在.NET框架下建造者模式編寫思路的實(shí)現(xiàn),需要的朋友可以參考下2016-02-02
C#四舍五入MidpointRounding.AwayFromZero解析
這篇文章主要介紹了C#四舍五入MidpointRounding.AwayFromZero,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
VS2019 找不到資產(chǎn)文件 “xxxx\obj\project.assets.json”運(yùn)行NuGet包還原以生成此文
這篇文章主要介紹了VS2019 找不到資產(chǎn)文件 “xxxx\obj\project.assets.json”運(yùn)行NuGet包還原以生成此文件,本文給大家分享解決方案,感興趣的朋友跟隨小編一起學(xué)習(xí)吧2020-08-08

