我一直在尝试截取屏幕截图,然后立即使用它来显示某种预览,某些时候它可以工作,有时甚至不工作,我目前没有工作,我没有在这台电脑的统一,所以我会尝试重新创建它在飞行中(有可能是这里的一些语法错误并有)使用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-屏幕截图),但是你提出的这个事实让我感觉更好,明天我会试试看,谢谢! –