Unity實現截屏以及根據相機畫面截圖
更新時間:2020年04月18日 11:04:24 作者:程序猴子sy
這篇文章主要為大家詳細介紹了Unity實現截屏以及根據相機畫面截圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
在游戲開發(fā)和軟件開發(fā)中,經常需要截圖的功能,分帶UI的截圖和不帶UI的截圖功能。代碼如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; public static class ScreenShotForCamera{ public static void CaptureScreen(string _path = null) { if (_path == null) _path = "Screenshot.png"; Application.CaptureScreenshot(_path, 0); } public static Texture2D CaptureScreen(Rect rect, bool _isCreatePhoto = false, string _path = null) { // 先創(chuàng)建一個的空紋理,大小可根據實現需要來設置 Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false); // 讀取屏幕像素信息并存儲為紋理數據, screenShot.ReadPixels(rect, 0, 0); screenShot.Apply(); // 然后將這些紋理數據,成一個png圖片文件 if (_isCreatePhoto) { if(_path == null) _path = Application.dataPath + "/Screenshot.png"; byte[] bytes = screenShot.EncodeToPNG(); string filename = _path; System.IO.File.WriteAllBytes(filename, bytes); Debug.Log(string.Format("截屏了一張圖片: {0}", filename)); } // 最后,我返回這個Texture2d對象,這樣我們直接,所這個截圖圖示在游戲中,當然這個根據自己的需求的。 return screenShot; } // public static Texture2D CaptureCamera(ref Camera _camera, Rect _rect, int _destX, int _destY, bool _isCreatePhoto = false, string _path = null) { RenderTexture renderTexture = new RenderTexture((int)_rect.width, (int)_rect.height, 24, RenderTextureFormat.ARGB32); _camera.targetTexture = renderTexture; _camera.Render(); // 激活這個renderTexture, 并從中中讀取像素 RenderTexture.active = _camera.targetTexture; Texture2D screenShot = new Texture2D((int)_rect.width, (int)_rect.height, TextureFormat.ARGB32, false); screenShot.ReadPixels(_rect, _destX, _destY); //從(_destX,_destY)坐標開始讀取_rect大小的圖片 screenShot.Apply(); //重置參數 //_camera.targetTexture = null; RenderTexture.active = null; //GameObject.Destroy(renderTexture); //生成PNG圖片 if (_isCreatePhoto) { if (_path == null) _path = Application.dataPath + "/Screenshot.png"; byte[] bytes = screenShot.EncodeToPNG(); string filename = _path; System.IO.File.WriteAllBytes(filename, bytes); Debug.Log(string.Format("截屏了一張照片: {0}", filename)); } return screenShot; } }
小編再為大家分享一段:Unity實現截屏功能,希望可以幫到大家
public class ScreenShot : MonoBehaviour { void OnScreenShotClick() { //得到當前系統時間 System.DateTime now = System.DateTime.Now; string times = now.ToString(); //去掉前后空格 times = times.Trim(); //將斜杠替換成橫杠 times = times.Replace("/", "-"); string fileName = "ARScreenShot" + times + ".png"; //判斷該平臺是否為安卓平臺 if (Application.platform == RuntimePlatform.Android) { //參數依次為 屏幕寬度 屏幕高度 紋理格式 是否使用映射 Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); //讀取貼圖 texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); //應用截屏 texture.Apply(); //將對象序列化 byte[] bytes = texture.EncodeToPNG(); //設定存儲到的手機文件夾路徑 string destination = "/sdcard/DCIM/Screenshots"; //如果不存在該文件夾 if (!Directory.Exists(destination)) { //創(chuàng)建該文件夾 Directory.CreateDirectory(destination); } string pathSave = destination + "/" + fileName; File.WriteAllBytes(pathSave, bytes); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C#多線程之Thread中Thread.IsAlive屬性用法分析
這篇文章主要介紹了C#多線程之Thread中Thread.IsAlive屬性用法,實例分析了C#判斷線程可用狀態(tài)的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04