使用ScreenCapture.CaptureScreenshot捕获并保存屏幕截图 [英] Capture and save screenshot with ScreenCapture.CaptureScreenshot

查看:904
本文介绍了使用ScreenCapture.CaptureScreenshot捕获并保存屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试拍摄截图,然后立即使用它来显示某种预览,有时它会起作用,有时却没有,我目前没有上班,我不这样做在这台计算机上有统一性,所以我会尝试在运行中重新创建它(这里和那里可能存在一些语法错误)

I've been trying to take a screenshot and then immediately after, use it to show some sort of preview and some times it works and some times it doesn't, I'm currently not at work and I don't have unity in this computer so I'll try to recreate it on the fly (there might be some syntax mistakes here and there)

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纹理,这显然是纹理无法加载,但文件应该在那里,所以我无法理解为什么它没有正确加载。

As far as I've read, ScreenCapture.CaptureScreenshot is not async so the image should have been written right before I try to load the data, but the problem is as I've said before some times it doesn't work and it loads an 8x8 texture with a red question mark, which apparently is the texture failing to be loaded but the file should've been there so I cannot understand why it's not getting loaded properly.

我尝试过的另一件事(这是令人作呕的但是我已经厌倦了这个并且耗尽了想法)是在更新方法中等待一段时间然后执行代码来加载数据并创建纹理,精灵并显示它但即便如此,它也会失败有时候,比以前更少,但它仍然失败,这让我相信即使文件创建它还没有完成beign写,有没有人知道解决这个问题?任何建议都表示赞赏。

another thing I've tried (which is disgusting but I'm getting tired of this and running out of ideas) is to put in the update method to wait for some time and then execute the code to load the data and create the texture, sprite and display it but even so, it fails some times, less frequently than before but it still fails, which leads me to belive that even if the file was created it hasn't finish beign written, does anyone know a workaround to this? any advice is appreciated.

作为额外信息,此项目正在iOS设备上运行。

As extra information this project is being run in an iOS device.

推荐答案

已知 ScreenCapture.CaptureScreenshot 函数存在许多问题。 这里是另一个。

The ScreenCapture.CaptureScreenshot function is known to have many problems. Here is another one of it.

以下是 doc 的引用:


在Android上,此函数立即返回。生成的屏幕截图
稍后可用。

On Android this function returns immediately. The resulting screenshot is available later.

iOS行为未记录,但我们可以假设行为是 - 在iOS上相同。在尝试读取/加载屏幕截图之前等待几帧后。

The iOS behavior is not documented but we can just assume that the behavior is the-same on iOS. Wait for few frames after taking the screenshot before you attempt to read/load it.

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()); 来调用此函数。

Note that you must use StartCoroutine(TakeScreenshot()); to call this function.

如果这不起作用,根本不要使用此功能。以下是在Unity中获取和保存屏幕截图的另一种方法:

If that did not work, don't use this function at-all. Here is another way to take and save screenshot in Unity:

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);
}

这篇关于使用ScreenCapture.CaptureScreenshot捕获并保存屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆