Capture and save screenshot with ScreenCapture.CaptureScreenshot

前端 未结 3 601
感动是毒
感动是毒 2021-01-15 19:14

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

3条回答
  •  春和景丽
    2021-01-15 19:29

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

    Here is a quote from its doc:

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

    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().sprite = screenshotSprite;
    
    }
    

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


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

提交回复
热议问题