Unity-截圖工具類
阿新 • • 發佈:2018-12-19
實現功能: 1.採用Texture方式截圖; 2.截圖儲存在 Application.persistentDataPath目錄下; 3.圖片轉bsde64碼,易於傳輸;
/**************************************************** * * unity螢幕截圖,並轉換成Base64碼 * *****************************************************/ using System; using System.IO; using UnityEngine; using System.Collections; public class Tools_TexScreenshot : MonoBehaviour { public delegate void WeChatTexScreenShotDelegate(bool isSucc); public WeChatTexScreenShotDelegate WeChatTexScreenShotCallBack; private static Tools_TexScreenshot _Instance; public static Tools_TexScreenshot Instance { get { return _Instance; } } void Awake() { _Instance = this; } void OnDestroy() { _Instance = null; Texture_ShotDelete(); } /// <summary> /// 截圖圖片儲存路徑 /// </summary> private string texShotPath { get { throw new NotImplementedException("TODO 截圖圖片儲存路徑"); //return projectQ.PathHelper.PersistentPath + "/screencapture.jpg";//TODO 截圖圖片儲存路徑 } } /// <summary> /// 壓縮後的截圖圖片儲存路徑 /// </summary> private string texZipShotPath { get { throw new NotImplementedException("TODO 壓縮後的截圖圖片儲存路徑"); //return projectQ.PathHelper.PersistentPath + "/texShare.jpg";//TODO 壓縮後的截圖圖片儲存路徑 } } public byte[] GetCaptureTexture(Texture2D _tex2d) { _tex2d.Compress(false); Texture2D texZip = Tools_TexGzip.TexGzipBegin(_tex2d, 0.5f); return texZip.EncodeToJPG(); } /// <summary> /// 螢幕截圖儲存 /// </summary> /// <param name="startPoint">截圖起點座標</param> /// <param name="shotSize">截圖大小</param> public void Texture_Screenshot(Vector2 startPoint, Vector2 shotSize, WeChatTexScreenShotDelegate shotBack) { StartCoroutine(GetCapture(startPoint, shotSize, shotBack)); } IEnumerator GetCapture(Vector2 startPoint, Vector2 shotSize, WeChatTexScreenShotDelegate shotBack) { // 截圖之前,刪除截圖快取檔案 Texture_ShotDelete(); yield return new WaitForEndOfFrame(); int width = (int)shotSize.x; int height = (int)shotSize.y; Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false); tex.ReadPixels(new Rect(startPoint.x, startPoint.y, width, height), 0, 0, true); //對螢幕快取進行壓縮 tex.Compress(false); //對圖片進行壓縮 Texture2D texZip = Tools_TexGzip.TexGzipBegin(tex, 0.5f); //轉化為jpg圖 byte[] imagebytes = texZip.EncodeToJPG(); //儲存png圖 File.WriteAllBytes(texShotPath, imagebytes); yield return null; if (shotBack != null) { shotBack(true); } } /// <summary> /// 圖片壓縮 /// </summary> void Texture_Gzip(int height, int width, WeChatTexScreenShotDelegate shotBack) { if (shotBack != null) { shotBack(true); } } /// <summary> /// 將圖片資料轉換為Base64字串 /// </summary> public string Texture_ToBase64() { if (!System.IO.File.Exists(texShotPath)) { Debug.Log(" 圖片路徑無效: " + texShotPath); return null; } var base64Img = Convert.ToBase64String(System.IO.File.ReadAllBytes(texShotPath)); Debug.Log(" #[圖片轉換成Base64]# 成功 圖片資訊" + base64Img); // 分享完成,圖片刪除 Texture_ShotDelete(); return base64Img.ToString(); } /// <summary> /// 將指定路徑圖片轉換為Base64字串 /// </summary> public string Texture_FixedToBase64(string texFixedPath) { var base64Img = Convert.ToBase64String(System.IO.File.ReadAllBytes(texFixedPath)); return base64Img.ToString(); } /// <summary> /// 將本地圖片轉換為Base64字串 /// </summary> public string Texture_LocalToBase64(byte[] _bytes) { var base64Img = Convert.ToBase64String(_bytes); return base64Img.ToString(); } /// <summary> /// 將儲存下來的用於分享的圖片刪除掉 /// </summary> public void Texture_ShotDelete() { if (File.Exists(texShotPath)) { Debug.Log(" #[如果截圖的圖片還在則刪掉]# "); File.Delete(texShotPath); } if (File.Exists(texZipShotPath)) { Debug.Log(" #[圖片轉換成Base64成功後刪除]# "); File.Delete(texZipShotPath); } } }