1. 程式人生 > 程式設計 >unity實現QQ截圖功能

unity實現QQ截圖功能

本文例項為大家分享了unity實現QQ截圖功能的具體程式碼,供大家參考,具體內容如下

效果:

unity實現QQ截圖功能

程式碼如下:

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;
 //繪製狀態
 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;
   }
   //滑鼠處於按下狀態時
   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));
    //開啟繪製的協程方法
    StartCoroutine(Capsture(rect));
    stateDraw = false;
    stateDrawStart = false;
   }
  }
 }
 /// <summary>
 /// 儲存截圖
 /// </summary>
 /// <param name="rect"></param>
 /// <returns></returns>
 IEnumerator Capsture(Rect rect)
 {
  yield return new WaitForEndOfFrame();

  //建立紋理(紋理貼圖的大小和截圖的大小相同)
  Texture2D tex = new Texture2D((int)rect.width,(int)rect.height,TextureFormat.RGB24,false);
  //讀取畫素點
  tex.ReadPixels(rect,0);
  //將畫素點應用到紋理上,繪製圖片
  tex.Apply();
  //將圖片裝換成jpg的二進位制格式,儲存在byte陣列中(計算機是以二進位制的方式儲存資料)
  byte[] result = tex.EncodeToPNG();
  //資料夾(如果StreamAssets資料夾不存在,在Assets檔案下建立該資料夾)
  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,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();
 }

 public void OnBtnClick() {
  stateDraw = true;
 }
}

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