2

I'm trying to create some screenshots but ScreenCapture.CaptureScreenshot actually captures the entire editor and not just the Game view.

error

public class ScreenShotTaker : MonoBehaviour
{
    public KeyCode takeScreenshotKey = KeyCode.S;
    public int screenshotCount = 0;
    private void Update()
    {
        if (Input.GetKeyDown(takeScreenshotKey))
        {
            ScreenCapture.CaptureScreenshot("Screenshots/"
                 + "_" + screenshotCount + "_"+ Screen.width + "X" +     Screen.height + "" + ".png");
            Debug.Log("Screenshot taken.");
        }
    }
}    

What could be the issue? How to take a decent, game view only screenshot that includes the UI?

Note, the UI thing, I found other methods online to take a screenshot (using RenderTextures) but those didn't include the UI. In my other, "real" project I do have UI as well, I just opened this tester project to see if the screenshot issue persists here too.

agiro
  • 2,018
  • 2
  • 30
  • 62

1 Answers1

4

This is a bug and I suggest you stay away from it for while until ScreenCapture.CaptureScreenshot is mature enough. This function was added in Unity 2017.2 beta so this is the right time to file for a bug report from the Editor. To make it worse, it saves only black and blank image on my computer.


As for taking screenshots, there other ways to do this without RenderTextures, that will also include the UI in the screenshot too.

You can read pixels from the screen with Texture2D.ReadPixels then save it with File.WriteAllBytes.

public KeyCode takeScreenshotKey = KeyCode.S;
public int screenshotCount = 0;

private void Update()
{
    if (Input.GetKeyDown(takeScreenshotKey))
    {
        StartCoroutine(captureScreenshot());
    }
}

IEnumerator captureScreenshot()
{
    yield return new WaitForEndOfFrame();
    string path = "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);
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thanks, it mostly works. The only issue is that my UI elements use `Outline` and the outline values are always their default white. I change them during initialization, but the screenshot ignores it. For now, shall I just change them manually? (The reason for automated coloring is that they get colour from the level materials to match up). – agiro Aug 25 '17 at 17:34
  • All in all though, still the best solution I have seen out there. – agiro Aug 25 '17 at 17:36
  • And yes, I did file a bug report :D hopefully they fix this soon. – agiro Aug 25 '17 at 17:42
  • Yes that. By the way, I checked the results in a 2D app and found that it ain't white, it's transparent. So I saved into a `jpeg` and everything works just fine. By the way, I need these - as you might have guessed - to upload to Google Play. If I Google up some phone's resolution and capture screenshots in those resolutions, will that do for the _phone_ and _tablet_ required screenshots? – agiro Aug 25 '17 at 17:47