How to take a screenshot in Unity?

Unity provides us with a method called ScreenCapture.CaptureScreenshot() that allows us to save the camera image to a PNG file:

 
 



ScreenshotTaker script:

Create a new script:

 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScreenshotTaker : MonoBehaviour
{
    
}

TakeAScreenshot method

 

First to five our file a name that makes sense I want to use the current Date and Time:

ex Screenshot_20220125_111941.png

To do this we get the current data and time as string:

 
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScreenshotTaker : MonoBehaviour
{
    public void TakeAScreenshot()
    {
        string currentDateTime =
            DateTime.Now.ToString("yyyyMMdd_Hmmss");
    }

}

We need to add using System; at the top to use DateTime class

 

Next we need to define a path where to save the screenshots. We will start by defining a directory:

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class ScreenshotTaker : MonoBehaviour
{
    public void TakeAScreenshot()
    {
        string currentDateTime =
            DateTime.Now.ToString("yyyyMMdd_Hmmss");

        //You may want to use Application.persistentDataPath
        var directory =
            new DirectoryInfo(Application.dataPath + "\\Assets");
    }

}

The DirectoryInfo will allow us to get the absolute path. In windows the Application.persistentDataPath returns %userprofile%\AppData\Local\Packages\<productname>\LocalState which doesn’t play nicely with Windows OS - you may find an empty folder if you use it directly. That is why we want to access the directory and gets its path. You can also check if the directory exists to avoid errors.

Now we will combine everything to create an absolute path that Unity can use to save our screenshot to:

 
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class ScreenshotTaker : MonoBehaviour
{
    public void TakeAScreenshot()
    {
        string currentDateTime =
            DateTime.Now.ToString("yyyyMMdd_Hmmss");

        //You may want to use Application.persistentDataPath
        var directory =
            new DirectoryInfo(Application.dataPath + "\\Assets");

        var path = Path.Combine(
            directory.Parent.FullName,
            $"Screenshot_{currentDateTime}.png");

        Debug.Log(path);

        ScreenCapture.CaptureScreenshot(path);
    }

}

Path.Combine will create for us an absolute path by combining the directory path with our name consisting of “Screenshot+” and the currentDateTime string -> $"Screenshot_{currentDateTime}.png")

To save the screenshot unity has a special method called ScreenCapture.CaptureScreenshot(path);

*The resolution will be the same as the resolution set in the Game view so if you have Free Aspect your resolution might not be something strange like 1164x605.

Now last thing it to add code that will allow you to test the logic ex by pressing “K” you will take a screenshot:

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class ScreenshotTaker : MonoBehaviour
{

    ...


    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.K))
        {
            TakeAScreenshot();
        }
    }
}


And now you should be able to test your code in Unity

 

If in Unity the resolution has some strange value you must swap the texture type to sprite.

Want to learn more about making 2D games in Unity ?

To learn more on how to improve the way you write code by making 2D games from scratch check out my Make a 2D platformer using Design Patterns video courses :

 
 

You can also support me through Patreon:

 

If you agree or disagree let me know by joining the Sunny Valley Studio discord channel :)

Thanks for reading!

Peter

Previous
Previous

How to select 2D objects using mouse in Unity?

Next
Next

How to play a different step sound based on the terrain type in a 2D top down game?