欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity實(shí)現(xiàn)截屏以及根據(jù)相機(jī)畫面截圖

 更新時(shí)間:2020年04月18日 11:04:24   作者:程序猴子sy  
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)截屏以及根據(jù)相機(jī)畫面截圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在游戲開發(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)文章

最新評(píng)論