1. 程式人生 > 程式設計 >Unity實現全屏截圖以及QQ截圖

Unity實現全屏截圖以及QQ截圖

本文例項為大家分享了Unity實現全屏截圖、Unity實現QQ截圖,供大家參考,具體內容如下

全屏截圖:要實現的是點選滑鼠左鍵,就實現截圖,並且將所截圖片儲存到本地Assets目錄下的StreamingAssets資料夾下面。
程式碼如下:

using UnityEngine;
using System.Collections;

public class TakeScreenShot : MonoBehaviour {
void Update () {
//點選滑鼠左鍵
if (Input.GetMouseButtonDown (0)) {
//開啟協程方法
StartCoroutine (MyCaptureScreen ());

}
}
//我的截圖方法
IEnumerator MyCaptureScreen(){
//等待所有的攝像機和GUI被渲染完成。
yield return new WaitForEndOfFrame ();
//建立一個空紋理(圖片大小為螢幕的寬高)
Texture2D tex = new Texture2D (Screen.width,Screen.height);
//只能在幀渲染完畢之後呼叫(從螢幕左下角開始繪製,繪製大小為螢幕的寬高,寬高的偏移量都為0)
tex.ReadPixels (new Rect (0,Screen.width,Screen.height),0);
//圖片應用(此時圖片已經繪製完成)
tex.Apply ();
//將圖片裝換成jpg的二進位制格式,儲存在byte陣列中(計算機是以二進位制的方式儲存資料)
byte[] result = tex.EncodeToJPG ();
//檔案儲存,建立一個新檔案,在其中寫入指定的位元組陣列(要寫入的檔案的路徑,要寫入檔案的位元組。)
System.IO.File.WriteAllBytes (Application.streamingAssetsPath+"/1.JPG",result);

}
}

之後思考一下,如何像qq截圖那樣,繪製一個截圖的矩形框只擷取自己想要的部分呢?

using UnityEngine;
using System.Collections;
using System.IO;//引入IO流
public class NewBehaviourScript : MonoBehaviour {
//定義一個儲存截圖圖片的路徑
string filePath;
void Start () {
//圖片儲存在StreamingAssets資料夾內。
filePath = Application.streamingAssetsPath + "/1.png";
}
Rect rect;
//截圖開始的位置
Vector3 s_pos;
//截圖結束的位置
Vector3 e_pos;
//是否繪製
bool isDraw;
void Update()
{
//按下滑鼠左鍵時,記錄當前滑鼠的位置為開始截圖時的位置
if(Input.GetMouseButtonDown(0))
{
s_pos = Input.mousePosition;
}
//滑鼠處於按下狀態時
if(Input.GetMouseButton(0))
{
e_pos = Input.mousePosition;
//可以開始繪製
isDraw = true;
}
//抬起滑鼠左鍵時,記錄當前滑鼠的位置為結束截圖時的位置
if(Input.GetMouseButtonUp(0))
{
//結束繪製
isDraw = false;
e_pos = Input.mousePosition;
//獲取到截圖框起始點的位置,和寬高。
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));
//開啟繪製的協程方法
StartCoroutine(Capsture(filePath,rect));
}
}


IEnumerator Capsture(string filePath,Rect rect)
{
yield return new WaitForEndOfFrame();
//建立紋理(紋理貼圖的大小和截圖的大小相同)
Texture2D tex = new Texture2D((int)rect.width,(int)rect.height);
//讀取畫素點
tex.ReadPixels(rect,0) ;
//將畫素點應用到紋理上,繪製圖片
tex.Apply();
//將圖片裝換成jpg的二進位制格式,儲存在byte陣列中(計算機是以二進位制的方式儲存資料)
byte[] result =tex.EncodeToPNG();
//資料夾(如果StreamAssets資料夾不存在,在Assets檔案下建立該資料夾)
if(!Directory.Exists(Application.streamingAssetsPath))
Directory.CreateDirectory(Application.streamingAssetsPath);
//將截圖圖片儲存到本地
File.WriteAllBytes(filePath,result);
}
//在這裡要用GL實現繪製截圖的矩形框
//1.GL的回撥函式
//2.定義一個材質Material
public Material lineMaterial;

void OnPostRender() {
if(!isDraw) return;
print (s_pos);

Vector3 sPos = Camera.main.ScreenToWorldPoint(s_pos + new Vector3(0,10));
Vector3 ePos = Camera.main.ScreenToWorldPoint(e_pos + new Vector3(0,10));

print(string.Format("GL.....{0},{1}",sPos,ePos));
// Set your materials Done
GL.PushMatrix();
// yourMaterial.SetPass( );
lineMaterial.SetPass(0);//告訴GL使用該材質繪製
// 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,0));
GL.Vertex(ePos);

GL.Vertex(ePos);
GL.Vertex(new Vector3(sPos.x,ePos.y,0));

GL.Vertex(new Vector3(sPos.x,0));
GL.Vertex(sPos);
GL.End();//結束繪製

GL.PopMatrix();
}

}

在指令碼元件中拖入材質球

Unity實現全屏截圖以及QQ截圖

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

Unity實現全屏截圖以及QQ截圖

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。