2017-09-29 176 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)屈服?您也可以尝试在其中引入一些调试检查,以查看文件出现之前需要多长时间(秒或帧)(假设它出现)。