截圖3種方法
方法1 Application.CaptureScreenshot()
Application.CaptureScreenshot(screencapture.png)
if(Application.platform==RuntimePlatform.Android || Application.platform==RuntimePlatform.IPhonePlayer)
imagePath=Application.persistentDataPath;
else if(Application.platform==RuntimePlatform.WindowsPlayer)
imagePath=Application.dataPath;
else if(Application.platform==RuntimePlatform.WindowsEditor)
{
imagePath=Application.dataPath;
imagePath= imagePath.Replace("/Assets",null);
}
imagePath = imagePath + "/screencapture.png";
方法2 Texture2d類下的相關(guān)方法,通過(guò)讀取屏幕緩存然后轉(zhuǎn)化為Png圖片進(jìn)行截圖
/// <summary>
/// Captures the screenshot2.
/// </summary>
/// <returns>The screenshot2.</returns>
/// <param name="rect">Rect.截圖的區(qū)域,左下角為o點(diǎn)</param>
Texture2D CaptureScreenshot2(Rect rect)
{
// 先創(chuàng)建一個(gè)的空紋理,大小可根據(jù)實(shí)現(xiàn)需要來(lái)設(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圖片文件
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/Screenshot.png";
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("截屏了一張圖片: {0}", filename));
// 最后,我返回這個(gè)Texture2d對(duì)象,這樣我們直接,所這個(gè)截圖圖示在游戲中,當(dāng)然這個(gè)根據(jù)自己的需求的。
return screenShot;
}
方法3 用RenderTexture針對(duì)某個(gè)相機(jī)進(jìn)行截圖
/// <summary>
/// 對(duì)相機(jī)截圖。
/// </summary>
/// <returns>The screenshot2.</returns>
/// <param name="camera">Camera.要被截屏的相機(jī)</param>
/// <param name="rect">Rect.截屏的區(qū)域</param>
Texture2D CaptureCamera(Camera camera, Rect rect)
{
// 創(chuàng)建一個(gè)RenderTexture對(duì)象
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
// 臨時(shí)設(shè)置相關(guān)相機(jī)的targetTexture為rt, 并手動(dòng)渲染相關(guān)相機(jī)
camera.targetTexture = rt;
camera.Render();
//ps: --- 如果這樣加上第二個(gè)相機(jī),可以實(shí)現(xiàn)只截圖某幾個(gè)指定的相機(jī)一起看到的圖像。
//ps: camera2.targetTexture = rt;
//ps: camera2.Render();
//ps: -------------------------------------------------------------------
// 激活這個(gè)rt, 并從中中讀取像素。
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
screenShot.ReadPixels(rect, 0, 0);// 注:這個(gè)時(shí)候,它是從RenderTexture.active中讀取像素
screenShot.Apply();
// 重置相關(guān)參數(shù),以使用camera繼續(xù)在屏幕上顯示
camera.targetTexture = null;
//ps: camera2.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
GameObject.Destroy(rt);
// 最后將這些紋理數(shù)據(jù),成一個(gè)png圖片文件
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/Screenshot.png";
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("截屏了一張照片: {0}", filename));
return screenShot;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。