[Unity-11] Unity截屏方法

本文轉自:http://blog.csdn.net/anyuanlzh/article/details/17008909

以下爲全文原文:


下面是我總結的、在u3d中的,三種截屏方法:

1、使用Application類下的CaptureScreenshot方法。

[csharp] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. void CaptureScreen()   
  2. {  
  3.     Application.CaptureScreenshot("Screenshot.png", 0);  
  4. }  

 

這個方法,截取的是某一幀時整個遊戲的畫面,或者說是全屏截圖吧。

a、不能針對某一個相機(camera)的畫面,進行截圖。

b、對局部畫面截圖,實現起來不方便,效率也低,不建議在項目中使用:

雖然CaptureScreenshot這個方法呢,本身是不要做到這一點的。但是我們可以走曲線救國的路線來實現它。思路是這樣的:你可以先用這個方法截圖一個全屏,然後通過路徑獲取到這個截圖;接下來就通過相關的圖形類來,取得這個截圖的局部區域並保存下來,這樣就能得到一個局部截圖了。在這裏我就不實現它了,不過有興趣的可以試試,肯定是可以實現的。


2、這第二個截圖的方法是,使用Texture2d類下的相關方法,也可實現截圖功能。

[csharp] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. /// <summary>  
  2. /// Captures the screenshot2.  
  3. /// </summary>  
  4. /// <returns>The screenshot2.</returns>  
  5. /// <param name="rect">Rect.截圖的區域,左下角爲o點</param>  
  6. Texture2D CaptureScreenshot2(Rect rect)   
  7. {  
  8.     // 先創建一個的空紋理,大小可根據實現需要來設置  
  9.     Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);  
  10.   
  11.     // 讀取屏幕像素信息並存儲爲紋理數據,  
  12.     screenShot.ReadPixels(rect, 0, 0);  
  13.     screenShot.Apply();  
  14.   
  15.     // 然後將這些紋理數據,成一個png圖片文件  
  16.     byte[] bytes = screenShot.EncodeToPNG();  
  17.     string filename = Application.dataPath + "/Screenshot.png";  
  18.     System.IO.File.WriteAllBytes(filename, bytes);  
  19.     Debug.Log(string.Format("截屏了一張圖片: {0}", filename));  
  20.   
  21.     // 最後,我返回這個Texture2d對象,這樣我們直接,所這個截圖圖示在遊戲中,當然這個根據自己的需求的。  
  22.     return screenShot;  
  23. }  
截全屏(如下圖, 注:有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類的方法,使用上也有一些要注意的地方,自己看吧。

當然,這個方法也不要到實現針對某個相機的截圖的功能。不過關鍵接口已經出現了,它就是Texture2d.ReadPixels(),這段就不說了,接着往下看吧!


3、這第三個方法,最牛了,可以針對某個相機進行截圖。

這樣的話,我就可截下,我的Avatar在遊戲中場景中所看的畫面了,UI界面(用一個專門的camera顯示)什麼的是不應該有的。要做到這一點,我們應該將分出一個camera來專門顯示ui界面,用另一個camera相機來場景顯示場景畫面。然後,我們只對場景相機進行截屏就是了。所以這關鍵點就是:如何實現對某個相機進行截屏了。這裏用到一個新的類是RenderTexture。

代碼如下:

[csharp] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. /// <summary>  
  2. /// 對相機截圖。   
  3. /// </summary>  
  4. /// <returns>The screenshot2.</returns>  
  5. /// <param name="camera">Camera.要被截屏的相機</param>  
  6. /// <param name="rect">Rect.截屏的區域</param>  
  7. Texture2D CaptureCamera(Camera camera, Rect rect)   
  8. {  
  9.     // 創建一個RenderTexture對象  
  10.     RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);  
  11.     // 臨時設置相關相機的targetTexture爲rt, 並手動渲染相關相機  
  12.     camera.targetTexture = rt;  
  13.     camera.Render();  
  14.         //ps: --- 如果這樣加上第二個相機,可以實現只截圖某幾個指定的相機一起看到的圖像。  
  15.         //ps: camera2.targetTexture = rt;  
  16.         //ps: camera2.Render();  
  17.         //ps: -------------------------------------------------------------------  
  18.   
  19.     // 激活這個rt, 並從中中讀取像素。  
  20.     RenderTexture.active = rt;  
  21.     Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);  
  22.     screenShot.ReadPixels(rect, 0, 0);// 注:這個時候,它是從RenderTexture.active中讀取像素  
  23.     screenShot.Apply();  
  24.   
  25.     // 重置相關參數,以使用camera繼續在屏幕上顯示  
  26.     camera.targetTexture = null;  
  27.         //ps: camera2.targetTexture = null;  
  28.     RenderTexture.active = null// JC: added to avoid errors  
  29.     GameObject.Destroy(rt);  
  30.     // 最後將這些紋理數據,成一個png圖片文件  
  31.     byte[] bytes = screenShot.EncodeToPNG();  
  32.     string filename = Application.dataPath + "/Screenshot.png";  
  33.     System.IO.File.WriteAllBytes(filename, bytes);  
  34.     Debug.Log(string.Format("截屏了一張照片: {0}", filename));  
  35.       
  36.     return screenShot;  
  37. }  

多的我就不說了,相關知識自己去找資料吧。

直接上圖了。

無ui的全屏圖:


只有ui的全屏圖:

有ui有場景的全屏圖(只指定這兩個相機哦,相關提示在代碼的“//ps”中):




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章