Unity實(shí)現(xiàn)截屏以及根據(jù)相機(jī)畫面截圖
在游戲開發(fā)和軟件開發(fā)中,經(jīng)常需要截圖的功能,分帶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)建一個(gè)的空紋理,大小可根據(jù)實(shí)現(xiàn)需要來設(shè)置 Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false); // 讀取屏幕像素信息并存儲(chǔ)為紋理數(shù)據(jù), screenShot.ReadPixels(rect, 0, 0); screenShot.Apply(); // 然后將這些紋理數(shù)據(jù),成一個(gè)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)); } // 最后,我返回這個(gè)Texture2d對(duì)象,這樣我們直接,所這個(gè)截圖圖示在游戲中,當(dāng)然這個(gè)根據(jù)自己的需求的。 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(); // 激活這個(gè)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)坐標(biāo)開始讀取_rect大小的圖片 screenShot.Apply(); //重置參數(shù) //_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實(shí)現(xiàn)截屏功能,希望可以幫到大家
public class ScreenShot : MonoBehaviour { void OnScreenShotClick() { //得到當(dāng)前系統(tǒng)時(shí)間 System.DateTime now = System.DateTime.Now; string times = now.ToString(); //去掉前后空格 times = times.Trim(); //將斜杠替換成橫杠 times = times.Replace("/", "-"); string fileName = "ARScreenShot" + times + ".png"; //判斷該平臺(tái)是否為安卓平臺(tái) if (Application.platform == RuntimePlatform.Android) { //參數(shù)依次為 屏幕寬度 屏幕高度 紋理格式 是否使用映射 Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); //讀取貼圖 texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); //應(yīng)用截屏 texture.Apply(); //將對(duì)象序列化 byte[] bytes = texture.EncodeToPNG(); //設(shè)定存儲(chǔ)到的手機(jī)文件夾路徑 string destination = "/sdcard/DCIM/Screenshots"; //如果不存在該文件夾 if (!Directory.Exists(destination)) { //創(chuàng)建該文件夾 Directory.CreateDirectory(destination); } string pathSave = destination + "/" + fileName; File.WriteAllBytes(pathSave, bytes); } } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)判斷字符串中是否包含中文的方法
這篇文章主要介紹了C#實(shí)現(xiàn)判斷字符串中是否包含中文的方法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08c# 實(shí)現(xiàn)語音聊天的實(shí)戰(zhàn)示例
這篇文章主要介紹了c# 實(shí)現(xiàn)語音聊天的實(shí)戰(zhàn)示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02C#多線程之Thread中Thread.IsAlive屬性用法分析
這篇文章主要介紹了C#多線程之Thread中Thread.IsAlive屬性用法,實(shí)例分析了C#判斷線程可用狀態(tài)的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04C#使用密封類實(shí)現(xiàn)密封用戶信息的示例詳解
在C#中,密封類(sealed class)是一種不能被其他類繼承的類,它用于防止其他類繼承它的功能和屬性, 下面我們就來看看如何使用密封類密封用戶的信息吧2024-02-02微信跳一跳自動(dòng)腳本C#代碼實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了微信跳一跳自動(dòng)腳本C#代碼實(shí)現(xiàn)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01C# IQueryable<T>揭開表達(dá)式樹的神秘面紗
這篇文章主要介紹了C# IQueryable<T>表達(dá)式樹,對(duì)IQueryable<T>感興趣的同學(xué),必須要仔細(xì)看一下2021-04-04c#使用正則表達(dá)式匹配字符串驗(yàn)證URL示例
這篇文章主要介紹了c#使用正則表達(dá)式的小示例,匹配字符串、驗(yàn)證URL,大家參考使用吧2013-12-12Unity實(shí)現(xiàn)物體左右移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)物體左右移動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08