Unity实用小工具或脚本——录屏工具
一、前言
? ? ? ?本文要講的錄屏不是使用Unity自帶的那個截屏方法,因為unity自帶的都只能截取unity程序本身顯示的畫面內容,至于unity程序之外的內容,如電腦桌面上的其他的程序內容是無法錄屏的。本文所講的內容是采用C#本身的截屏方法,效果如圖所示:
unity錄屏工具示意圖使用的Unity版本為Unity2018.4.17。
二、實現
1、基本思路:先通過C#獲取到屏幕的數據,再轉換成Unity的Texture,獲取很簡單,主要是這個轉換的過程會比較的棘手。
2、獲取截圖數據代碼如下,獲取的是C#里面的Bitmap類型
Bitmap bit = new Bitmap(shotScreenRect.width,shotScreenRect.height);System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bit);g.CopyFromScreen(shotScreenRect.x, shotScreenRect.y, 0,0, bit.Size);先要轉換成字節(jié)數組Byte[],然后再轉成Texture,完整的代碼如下
using UnityEngine; using UnityEngine.UI; using System.Drawing; using System.IO; using System; using System.Threading; using System.Drawing.Imaging;public class CaptureScreen : MonoBehaviour {public RawImage shotShowImage;//顯示視頻的組件public RectInt shotScreenRect;//截圖的區(qū)域和寬高private Thread threadGetData;//截圖的線程private static byte[] ShotScreenData;//截圖的臨時數據數組private static bool LoadImageOver = false;//沒截張圖要等到下一次加載完void Start(){threadGetData = new Thread(new ThreadStart(Thread_GetScreenData));threadGetData.IsBackground = true;threadGetData.Start();}// Update is called once per framevoid Update(){//將截取的圖片的數據轉換成Texture2Dif (ShotScreenData != null&&LoadImageOver){Texture2D tempTex = new Texture2D(shotScreenRect.width, shotScreenRect.height);tempTex.LoadImage(ShotScreenData);shotShowImage.texture = tempTex;shotShowImage.SetNativeSize();LoadImageOver = false;}if(Input.GetKeyDown(KeyCode.Escape)){Application.Quit();}}/// <summary>/// 開啟線程截圖獲得數據/// </summary>private void Thread_GetScreenData(){try{while (true){if (!LoadImageOver){MemoryStream ms = new MemoryStream();CompressImage(GetScreen(), ms);ShotScreenData = ms.ToArray();LoadImageOver = true;}//Thread.Sleep(1000);}}catch (Exception ee){Debug.LogError(ee.Message);return;}}private void OnApplicationQuit(){if(null!= threadGetData)threadGetData.Abort(); }/// <summary>/// 截取一張圖片/// </summary>/// <returns></returns>private Bitmap GetScreen(){Bitmap bit = new Bitmap(shotScreenRect.width,shotScreenRect.height);System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bit);g.CopyFromScreen(shotScreenRect.x, shotScreenRect.y, 0,0, bit.Size);return bit;}/// <summary> /// 獲取圖片編碼信息 /// </summary> /// <param name="coderType">編碼類型</param> /// <returns>ImageCodecInfo</returns> private ImageCodecInfo getImageCoderInfo(string coderType){ImageCodecInfo[] iciS = ImageCodecInfo.GetImageEncoders();ImageCodecInfo retIci = null;foreach (ImageCodecInfo ici in iciS){if (ici.MimeType.Equals(coderType))retIci = ici;}return retIci;}/// <summary>/// 壓縮圖片/// </summary>/// <param name="bitmap"></param>/// <param name="ms"></param>private void CompressImage(Bitmap bitmap, MemoryStream ms){ImageCodecInfo ici = null;Encoder ecd = null;EncoderParameter ept = null;EncoderParameters eptS = null;try{ici = this.getImageCoderInfo("image/jpeg");ecd = Encoder.Quality;eptS = new EncoderParameters(1);ept = new EncoderParameter(ecd, 10L);eptS.Param[0] = ept;bitmap.Save(ms, ici, eptS);}catch (Exception ex){throw new Exception(ex.Message);}finally{ept.Dispose();eptS.Dispose();}}}3、這里使用的Bitmap類型以及其他相關的類都是在System.Drawing.dll的動態(tài)鏈接庫中,這個鏈接庫在unity的安裝文件中可以找到,但是要注意的是找到正確的版本,我因為一開始使用的是“Unity2018.4.17\Editor\Data\Mono\lib\mono\2.0”下的“System.Drawing.dll",在Editor模式下沒有問題,但是發(fā)布之后會報“?Fallback handler could not load library Mono/libc.dll”這樣的錯誤,看Log信息應該知道是dll加載的問題,無法加載。直到我一個個試才找到“Unity2018.4.17\Editor\Data\MonoBleedingEdge\lib\mono\unityjit”路徑下的這個“System.Drawing.dll"是可以用的版本。
4、最后是Unity的設置如圖
三、總結
1、使用unity自帶的截圖又一定的局限性,想要錄取其他的程序或電腦桌面的內容還是要借助C#本身的API;
2、動態(tài)鏈接庫的版本一定要控制好。
總結
以上是生活随笔為你收集整理的Unity实用小工具或脚本——录屏工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言-时间转换
- 下一篇: Office for Mac 2021