2017-09-29 177 views
0

我一直在嘗試截取屏幕截圖,然後立即使用它來顯示某種預覽,某些時候它可以工作,有時甚至不工作,我目前沒有工作,我沒有在這臺電腦的統一,所以我會嘗試重新創建它在飛行中(有可能是這裏的一些語法錯誤並有)使用ScreenCapture捕獲屏幕截圖並捕獲屏幕截圖.CaptureScreenshot

public GameObject screenshotPreview; 

public void TakeScreenshot() { 

     string imageName = "screenshot.png"; 

     // Take the screenshot 
     ScreenCapture.CaptureScreenshot (imageName); 

     // Read the data from the file 
     byte[] data = File.ReadAllBytes(Application.persistentDataPath + "/" + imageName); 

     // Create the texture 
     Texture2D screenshotTexture = new Texture2D(Screen.width, Screen.height); 

     // Load the image 
     screenshotTexture.LoadImage(data); 

     // Create a sprite 
     Sprite screenshotSprite = Sprite.Create (screenshotTexture, new Rect(0, 0, Screen.width, Screen.height), new Vector2(0.5f, 0.5f)); 

     // Set the sprite to the screenshotPreview 
     screenshotPreview.GetComponent<Image>().sprite = screenshotSprite; 

} 

至於我讀過, ScreenCapture.CaptureScreenshot不是異步的,所以圖像在我嘗試加載數據之前應該已經寫好了,但問題正如我之前所說的那樣,有時它不起作用,並且它加載了帶有紅色問號的8x8紋理,這顯然是紋理無法加載,但文件應該一直在那裏,所以我不明白爲什麼它沒有得到正確加載。

我嘗試過的另一件事(這是令人厭惡的,但我厭倦了這一點,並用儘想法)是放入更新方法等待一段時間,然後執行代碼來加載數據和創建紋理,精靈並顯示它,但即使如此,它失敗了一些次,比以前少了,但仍然失敗了,這讓我相信,即使文件被創建,它還沒有完成寫作,沒有人知道一個解決這個問題?任何建議表示讚賞。

作爲額外的信息,該項目正在iOS設備上運行。

回答

0

功能ScreenCapture.CaptureScreenshot已知有很多問題。 Here是另一個。

下面是它的doc報價:

在Android這個函數立即返回。生成的屏幕截圖 稍後可用。

iOS行爲沒有記錄,但我們可以假設iOS上的行爲是相同的。在嘗試讀取/加載之前,請在截取屏幕後等待幾幀。

public IEnumerator TakeScreenshot() 
{ 

    string imageName = "screenshot.png"; 

    // Take the screenshot 
    ScreenCapture.CaptureScreenshot(imageName); 

    //Wait for 4 frames 
    for (int i = 0; i < 5; i++) 
    { 
     yield return null; 
    } 

    // Read the data from the file 
    byte[] data = File.ReadAllBytes(Application.persistentDataPath + "/" + imageName); 

    // Create the texture 
    Texture2D screenshotTexture = new Texture2D(Screen.width, Screen.height); 

    // Load the image 
    screenshotTexture.LoadImage(data); 

    // Create a sprite 
    Sprite screenshotSprite = Sprite.Create(screenshotTexture, new Rect(0, 0, Screen.width, Screen.height), new Vector2(0.5f, 0.5f)); 

    // Set the sprite to the screenshotPreview 
    screenshotPreview.GetComponent<Image>().sprite = screenshotSprite; 

} 

請注意,您必須使用StartCoroutine(TakeScreenshot());來調用此函數。


如果這樣不起作用,請不要使用此函數。這裏是另一種方式在統一採取並保存截圖:

IEnumerator captureScreenshot() 
{ 
    yield return new WaitForEndOfFrame(); 

    string path = Application.persistentDataPath + "Screenshots/" 
      + "_" + screenshotCount + "_" + Screen.width + "X" + Screen.height + "" + ".png"; 

    Texture2D screenImage = new Texture2D(Screen.width, Screen.height); 
    //Get Image from screen 
    screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); 
    screenImage.Apply(); 
    //Convert to png 
    byte[] imageBytes = screenImage.EncodeToPNG(); 

    //Save image to file 
    System.IO.File.WriteAllBytes(path, imageBytes); 
} 
+0

我只是讀[這裏](https://developer.vuforia.com/forum/faq/unity-how-can-i-capture-屏幕截圖),但是你提出的這個事實讓我感覺更好,明天我會試試看,謝謝! –

0

我在文檔中沒有看到它說的不是異步。事實上,對於Android(如果我正確閱讀),它明確表示它是異步的。

這就是說,我會嘗試拖延,而沒有找到文件。把它扔進協同程序中,同時(!file.found)屈服?您也可以嘗試在其中引入一些調試檢查,以查看文件出現之前需要多長時間(秒或幀)(假設它出現)。