下面是我總結(jié)的、在u3d中的,三種截屏方法:
1、使用Application類下的CaptureScreenshot方法。
- void CaptureScreen()
- {
- Application.CaptureScreenshot("Screenshot.png", 0);
- }
這個方法,截取的是某一幀時整個游戲的畫面,或者說是全屏截圖吧。
a、不能針對某一個相機(jī)(camera)的畫面,進(jìn)行截圖。
b、對局部畫面截圖,實現(xiàn)起來不方便,效率也低,不建議在項目中使用:
雖然CaptureScreenshot這個方法呢,本身是不要做到這一點(diǎn)的。但是我們可以走曲線救國的路線來實現(xiàn)它。思路是這樣的:你可以先用這個方法截圖一個全屏,然后通過路徑獲取到這個截圖;接下來就通過相關(guān)的圖形類來,取得這個截圖的局部區(qū)域并保存下來,這樣就能得到一個局部截圖了。在這里我就不實現(xiàn)它了,不過有興趣的可以試試,肯定是可以實現(xiàn)的。
2、這第二個截圖的方法是,使用Texture2d類下的相關(guān)方法,也可實現(xià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)建一個的空紋理,大小可根據(jù)實現(xiàn)需要來設(shè)置
- Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
-
- // 讀取屏幕像素信息并存儲為紋理數(shù)據(jù),
- screenShot.ReadPixels(rect, 0, 0);
- screenShot.Apply();
-
- // 然后將這些紋理數(shù)據(jù),成一個png圖片文件
- byte[] bytes = screenShot.EncodeToPNG();
- string filename = Application.dataPath + "/Screenshot.png";
- System.IO.File.WriteAllBytes(filename, bytes);
- Debug.Log(string.Format("截屏了一張圖片: {0}", filename));
-
- // 最后,我返回這個Texture2d對象,這樣我們直接,所這個截圖圖示在游戲中,當(dāng)然這個根據(jù)自己的需求的。
- return screenShot;
- }
截全屏(如下圖, 注:有ui):CaptureScreenshot2( new Rect( Screen.width*0f, Screen.height*0f, Screen.width*1f, Screen.height*1f));
截中間4分之(如下圖):
CaptureScreenshot2( new Rect( Screen.width*0.25f, Screen.height*0.25f, Screen.width*0.5f, Screen.height*0.5f));
這里使用了幾個Texture2d類的方法,使用上也有一些要注意的地方,自己看吧。
當(dāng)然,這個方法也不要到實現(xiàn)針對某個相機(jī)的截圖的功能。不過關(guān)鍵接口已經(jīng)出現(xiàn)了,它就是Texture2d.ReadPixels(),這段就不說了,接著往下看吧!
3、這第三個方法,最牛了,可以針對某個相機(jī)進(jìn)行截圖。
這樣的話,我就可截下,我的Avatar在游戲中場景中所看的畫面了,UI界面(用一個專門的camera顯示)什么的是不應(yīng)該有的。要做到這一點(diǎn),我們應(yīng)該將分出一個camera來專門顯示ui界面,用另一個camera相機(jī)來場景顯示場景畫面。然后,我們只對場景相機(jī)進(jìn)行截屏就是了。所以這關(guān)鍵點(diǎn)就是:如何實現(xiàn)對某個相機(jī)進(jìn)行截屏了。這里用到一個新的類是RenderTexture。
代碼如下:
- /// <summary>
- /// 對相機(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)建一個RenderTexture對象
- RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
- // 臨時設(shè)置相關(guān)相機(jī)的targetTexture為rt, 并手動渲染相關(guān)相機(jī)
- camera.targetTexture = rt;
- camera.Render();
- //ps: --- 如果這樣加上第二個相機(jī),可以實現(xiàn)只截圖某幾個指定的相機(jī)一起看到的圖像。
- //ps: camera2.targetTexture = rt;
- //ps: camera2.Render();
- //ps: -------------------------------------------------------------------
-
- // 激活這個rt, 并從中中讀取像素。
- RenderTexture.active = rt;
- Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
- screenShot.ReadPixels(rect, 0, 0);// 注:這個時候,它是從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ù),成一個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;
- }
多的我就不說了,相關(guān)知識自己去找資料吧,因為我也不懂!
直接上圖了。
無ui的全屏圖:
只有ui的全屏圖:
有ui有場景的全屏圖(只指定這兩個相機(jī)哦,相關(guān)提示在代碼的“//ps”中):
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報。