本文轉自:http://blog.csdn.net/anyuanlzh/article/details/17008909
以下爲全文原文:
下面是我總結的、在u3d中的,三種截屏方法:
1、使用Application類下的CaptureScreenshot方法。
- void CaptureScreen()
- {
- Application.CaptureScreenshot("Screenshot.png", 0);
- }
這個方法,截取的是某一幀時整個遊戲的畫面,或者說是全屏截圖吧。
a、不能針對某一個相機(camera)的畫面,進行截圖。
b、對局部畫面截圖,實現起來不方便,效率也低,不建議在項目中使用:
雖然CaptureScreenshot這個方法呢,本身是不要做到這一點的。但是我們可以走曲線救國的路線來實現它。思路是這樣的:你可以先用這個方法截圖一個全屏,然後通過路徑獲取到這個截圖;接下來就通過相關的圖形類來,取得這個截圖的局部區域並保存下來,這樣就能得到一個局部截圖了。在這裏我就不實現它了,不過有興趣的可以試試,肯定是可以實現的。
2、這第二個截圖的方法是,使用Texture2d類下的相關方法,也可實現截圖功能。
- /// <summary>
- /// Captures the screenshot2.
- /// </summary>
- /// <returns>The screenshot2.</returns>
- /// <param name="rect">Rect.截圖的區域,左下角爲o點</param>
- Texture2D CaptureScreenshot2(Rect rect)
- {
- // 先創建一個的空紋理,大小可根據實現需要來設置
- Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
- // 讀取屏幕像素信息並存儲爲紋理數據,
- screenShot.ReadPixels(rect, 0, 0);
- screenShot.Apply();
- // 然後將這些紋理數據,成一個png圖片文件
- byte[] bytes = screenShot.EncodeToPNG();
- string filename = Application.dataPath + "/Screenshot.png";
- System.IO.File.WriteAllBytes(filename, bytes);
- Debug.Log(string.Format("截屏了一張圖片: {0}", filename));
- // 最後,我返回這個Texture2d對象,這樣我們直接,所這個截圖圖示在遊戲中,當然這個根據自己的需求的。
- return screenShot;
- }
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類的方法,使用上也有一些要注意的地方,自己看吧。
當然,這個方法也不要到實現針對某個相機的截圖的功能。不過關鍵接口已經出現了,它就是Texture2d.ReadPixels(),這段就不說了,接着往下看吧!
3、這第三個方法,最牛了,可以針對某個相機進行截圖。
這樣的話,我就可截下,我的Avatar在遊戲中場景中所看的畫面了,UI界面(用一個專門的camera顯示)什麼的是不應該有的。要做到這一點,我們應該將分出一個camera來專門顯示ui界面,用另一個camera相機來場景顯示場景畫面。然後,我們只對場景相機進行截屏就是了。所以這關鍵點就是:如何實現對某個相機進行截屏了。這裏用到一個新的類是RenderTexture。
代碼如下:
- /// <summary>
- /// 對相機截圖。
- /// </summary>
- /// <returns>The screenshot2.</returns>
- /// <param name="camera">Camera.要被截屏的相機</param>
- /// <param name="rect">Rect.截屏的區域</param>
- Texture2D CaptureCamera(Camera camera, Rect rect)
- {
- // 創建一個RenderTexture對象
- RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
- // 臨時設置相關相機的targetTexture爲rt, 並手動渲染相關相機
- camera.targetTexture = rt;
- camera.Render();
- //ps: --- 如果這樣加上第二個相機,可以實現只截圖某幾個指定的相機一起看到的圖像。
- //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();
- // 重置相關參數,以使用camera繼續在屏幕上顯示
- camera.targetTexture = null;
- //ps: camera2.targetTexture = null;
- RenderTexture.active = null; // JC: added to avoid errors
- GameObject.Destroy(rt);
- // 最後將這些紋理數據,成一個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;
- }
多的我就不說了,相關知識自己去找資料吧。
直接上圖了。
無ui的全屏圖:
只有ui的全屏圖:
有ui有場景的全屏圖(只指定這兩個相機哦,相關提示在代碼的“//ps”中):