我一直在嘗試截取屏幕截圖,然後立即使用它來顯示某種預覽,某些時候它可以工作,有時甚至不工作,我目前沒有工作,我沒有在這臺電腦的統一,所以我會嘗試重新創建它在飛行中(有可能是這裏的一些語法錯誤並有)使用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設備上運行。
我只是讀[這裏](https://developer.vuforia.com/forum/faq/unity-how-can-i-capture-屏幕截圖),但是你提出的這個事實讓我感覺更好,明天我會試試看,謝謝! –