unity實現(xiàn)QQ截圖功能
更新時間:2020年04月16日 10:54:23 作者:貪玩的孩紙時代
這篇文章主要為大家詳細介紹了unity實現(xiàn)QQ截圖功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了unity實現(xiàn)QQ截圖功能的具體代碼,供大家參考,具體內(nèi)容如下
效果:

代碼如下:
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using NPinyin;
using System.IO;
public class NewBehaviourScript : MonoBehaviour {
//截屏結束的位置
private Vector3 e_pos;
//是否繪制
private bool isDraw;
//繪制狀態(tài)
private bool stateDraw;
//開始繪制
private bool stateDrawStart;
//截屏開始的位置
private Vector3 s_pos;
Rect rect;
public Material lineMaterial;
private void Update()
{
if (stateDraw == true)
{
//按下鼠標左鍵時,記錄當前鼠標的位置為開始截屏時的位置
if (Input.GetMouseButtonDown(0))
{
stateDrawStart = true;
s_pos = Input.mousePosition;
}
//鼠標處于按下狀態(tài)時
if (Input.GetMouseButton(0))
{
e_pos = Input.mousePosition;
//可以開始繪制
isDraw = true;
}
//抬起鼠標左鍵時,記錄當前鼠標的位置為結束截屏時的位置
if (Input.GetMouseButtonUp(0) && stateDrawStart == true)
{
//結束繪制
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));
//開啟繪制的協(xié)程方法
StartCoroutine(Capsture(rect));
stateDraw = false;
stateDrawStart = false;
}
}
}
/// <summary>
/// 保存截圖
/// </summary>
/// <param name="rect"></param>
/// <returns></returns>
IEnumerator Capsture(Rect rect)
{
yield return new WaitForEndOfFrame();
//創(chuàng)建紋理(紋理貼圖的大小和截屏的大小相同)
Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
//讀取像素點
tex.ReadPixels(rect, 0, 0);
//將像素點應用到紋理上,繪制圖片
tex.Apply();
//將圖片裝換成jpg的二進制格式,保存在byte數(shù)組中(計算機是以二進制的方式存儲數(shù)據(jù))
byte[] result = tex.EncodeToPNG();
//文件夾(如果StreamAssets文件夾不存在,在Assets文件下創(chuàng)建該文件夾)
if (!Directory.Exists(Application.streamingAssetsPath))
Directory.CreateDirectory(Application.streamingAssetsPath);
//將截屏圖片存儲到本地
string filename = Application.dataPath + "/StreamingAssets/Screenshot.png";
File.WriteAllBytes(filename, result);
}
/// <summary>
/// 用GL畫線
/// </summary>
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();//結束繪制
GL.PopMatrix();
}
public void OnBtnClick() {
stateDraw = true;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C# 計算標準偏差相當于Excel中的STDEV函數(shù)實例
下面小編就為大家?guī)硪黄狢# 計算標準偏差相當于Excel中的STDEV函數(shù)實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
C# 大數(shù)據(jù)導出word的假死報錯的處理方法
C# 大數(shù)據(jù)導出word的假死報錯的處理方法,需要的朋友可以參考一下2013-03-03

