2016-01-22 126 views
0

我使用Unity3D黑屏捕捉截圖

Texture2D tex = new Texture2D(width,height); 
Rect textureRect = new Rect(0,0,width,height); 
tex.ReadPixels(textureRect,0,0); 
tex.Apply(); 

它正常工作,採取截圖。但是,當我在相機上應用圖像效果(如Unity標準資產中提供的模糊或綻放)時,截取的截圖爲黑色。我不懂爲什麼。有人可以幫助我嗎?

回答

1

使用Application.CaptureScreenshot拍攝截圖。

Application.CaptureScreenshot("Screenshot.png"); 

如果你想遊戲中顯示的話,我會建議使用RenderTexture,我認爲這是可以自Unity5免費。

+0

我想在指定給ReadPixels方法的屏幕上截取特定區域的屏幕截圖。 Application.CaptureScreenshot獲取整個攝像機的屏幕截圖。關於RenderTexture,我無法使用它。我只使用Camera作爲背景圖片,我必須在它上面放置幾個項目,這些項目是使用Unity 4.6中引入的UGUI放置的。這些項目具有渲染模式的屏幕空間覆蓋。我不想爲這些項目使用單獨的相機。 – kashif789us

+0

我用RenderTexture做了。我將一個RenderTexture分配給相機的目標紋理,並在我的Screen Overlay Canvas上將此RenderTexture顯示爲RawImage。然後採取screenhot和它的工作:) – kashif789us

0

您應該嘗試在OnPostRender()的任何組件中調用它。

而且你可以運行你捕捉代碼:Camera.OnRenderImage(RenderTexture,RenderTexture)

此方法由Unity3D引擎的所有渲染完成後,我們必須在屏幕上finall圖像後調用。

您需要爲其創建組件並將其附加到相機。

http://docs.unity3d.com/ScriptReference/Camera.OnRenderImage.html

+0

這兩種方法繼續無限調用。 – kashif789us

0
public IEnumerator SaveScreenshotRoutine(float width, float height, Action<Texture2D> result) 
{ 
    yield return new WaitForEndOfFrame(); 
    var tex = new Texture2D(width, height, TextureFormat.RGB24, false); 
    tex.ReadPixels(new Rect(0,0,width, height), 0, 0); 
    tex.Apply(); 
    result(tex); 
} 

您等待幀中要被完全完成,所以即使渲染完成,任何活動/非活動對象已經被添加/移除,並且使用它爲:

Texture2D texture = null; 
StartCoroutine(SaveScreenshotRoutine(result => texture =result)); 
+0

我在一個共同的例程中已經這樣做了,因爲當我像一個普通的方法那樣做時,它會給出錯誤。 – kashif789us