0
\$\begingroup\$

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

I'm using this few lines of code here:

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?

\$\endgroup\$
2
\$\begingroup\$

Took a look at it and it's actually a Unity bug, filed a report, see where it goes.

There are methods that rely on RenderTextures but they don't capture the UI. In the meantime, I asked this on Stack Overflow as well and got some handy answer that saves all pixel info from the screen like so:

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 + "" + ".jpeg";

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

Note that this script though works, it has to be modified for the sake of convenience. It also worths mentioning that if you use Outline on your UI elements, those will become transparent if saved as .png, so I used .jpeg instead.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.