How to Take a Screenshot from Within Your Game in Unity
It’s never a bad idea to provide your players with an easy way to take screenshots from within your game, without having to do it in a roundabout way.
Some platforms have a way to take screenshots of any game run on their platforms without the need to use external software. Steam, for example, let you press F12 to take a screenshot. But what if your game doesn’t use those platforms, or what if you want to take screenshots to do something, like setting thumbnails for the save files?
Taking a screenshot in a Unity game can be done by simply calling the CaptureScreenshot method of the ScreenCapture class, i.e. ScreenCapture.CaptureScreenshot(“<FilePath>/<FileName>”). This will create a screenshot of your game in PNG format at the path you specify.
There are various things you should look out for. I will go into more details on how to properly save screenshots below so make sure to keep reading!
Table of Contents
- How to Take a Screenshot in a Unity Game
- How to Save a Screenshot to a Specific Directory in Unity
- Saving a Screenshot to The Documents Directory
- Adding a Timestamp to a Screenshot
- Conclusion
How to Take a Screenshot in a Unity Game
A screenshot can easily be taken programmatically by calling the ScreenCapture.CaptureScreenshot method anytime. The screenshot generated will be of the screen you currently see in the game and will be in PNG format.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScreenshotManager : MonoBehaviour
{
public void TakeScreenshot()
{
ScreenCapture.CaptureScreenshot("TestImage.png");
}
}
Code language: C# (cs)
The code above will generate a screenshot named “TestImage.png” when the TakeScreenshot method is called. The resolution of the screenshot will be that of the current resolution the screenshot was taken on.
You can set the superSize parameter of the method to increase the size of the resulting screenshot:
ScreenCapture.CaptureScreenshot("TestImage.png", 3);
Code language: C# (cs)
The example above will make the screenshot 3 times bigger than the original. Note that it can only take an integer value.
As for where it is saved, if you’re testing this in the Unity Editor, the screenshot will be saved to the root directory of your game’s project folder.
For the standalone player, the screenshot will be saved to the “<your_game’s_name>_Data” directory inside your game’s directory.
This is not ideal, you want the player to be able to recognize where the screenshot should be. In the next section, I’ll be teaching you how to save screenshots to a specific directory.
How to Save a Screenshot to a Specific Directory in Unity
As mentioned previously, when only the file name is passed to the ScreenCapture.CaptureScreenshot method, the method will default the location of the screenshot to the root directory. We don’t want that.
Screenshots should be saved in a directory made to exclusively have screenshots only so they’re more organized.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class ScreenshotManager : MonoBehaviour
{
private string directoryName = "Screenshots";
private string fileName = "TestImage.png";
public void TakeScreenshot()
{
DirectoryInfo screenshotDirectory = Directory.CreateDirectory(directoryName);
string fullPath = Path.Combine(screenshotDirectory.FullName, fileName);
ScreenCapture.CaptureScreenshot(fullPath);
}
}
Code language: C# (cs)
The code above will try to create a directory named “Screenshots” if it doesn’t already exist and then save the screenshot with the name “TestImage.png” to that directory.
This might look a bit confusing so let me break it down for you.
using System.IO;
Code language: C# (cs)
This line needs to be included to make any class and method related to reading and writing data available. For more information about System.IO, see the System.IO Namespace official documentation
Code language: C# (cs)DirectoryInfo screenshotDirectory = Directory.CreateDirectory(directoryName);
The Directory.CreateDirectory method creates a directory with the specified name at the current path. In this case, it will create a directory named “Screenshots” at the root directory of your game, i.e. <your_game_directory>/Screenshots.
The reason we have to store the returned value (the DirectoryInfo object) is so we can get the full path to the created directory to be used later.
If the directory already exists, it will just return the value without creating a new directory.
string fullPath = Path.Combine(screenshotDirectory.FullName, fileName);
Code language: C# (cs)
The Path.Combine method joins multiple path names together. The FullName property of the screenshotDirectory object gives you the absolute path to the screenshot directory.
In this case, it will combine the absolute path to the directory with the file’s name which is:
<path_to_your_game>\Screenshots\TestImage.png
Code language: HTML, XML (xml)
Alternatively, you can combine the path and the file name by adding the 2 string values together using the “+” operator like this:
screenshotDirectory.FullName + "/" + fileName
Code language: C# (cs)
This will achieve the same result but I recommend using the Path.Combine method as it is cleaner and less error-prone.
Why do we have to create a directory first?
We have to create the directory we want to store screenshots in first because, otherwise, the ScreenCapture.CaptureScreenshot method will fail as it can’t save the file to a directory that doesn’t exist.
Code language: C# (cs)ScreenCapture.CaptureScreenshot(fullPath);
Lastly, we call the ScreenCapture.CaptureScreenshot method and pass the full path to it to save the screenshot.
Saving a Screenshot to The Documents Directory
Sometimes, it’s better to save screenshots to the Documents directory instead as it is easier to find and navigate to.
You might think that getting the path to the Documents directory is tricky as we need to know the user name of the current user. But don’t worry, there’s a way to do that.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
public class ScreenshotManager : MonoBehaviour
{
private string directoryName = "Screenshots";
private string screenshotName = "TestImage.png";
public void SaveScreenshotToDocuments()
{
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string screenshotPath = Path.Combine(documentsPath, Application.productName, directoryName);
DirectoryInfo screenshotDirectory = Directory.CreateDirectory(screenshotPath);
ScreenCapture.CaptureScreenshot(Path.Combine(screenshotDirectory.FullName, screenshotName));
}
}
Code language: C# (cs)
Let me explain a bit more in detail.
using System;
Code language: C# (cs)
This line need to be included to make the Environment namespace available to be used.
Code language: C# (cs)Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
The Environment.GetFolderPath method is used together with the Environment.SpecialFolder.MyDocuments enum value to get the Documents directory of the current user. For more information, refer to the official documentation of the Environment class.
string screenshotPath = Path.Combine(documentsPath, Application.productName, directoryName);
// screenshotPath = "<path_to_documents>/<game_name>/Screenshots"
Code language: C# (cs)
We then combine the path to the Documents directory with our game’s name and the screenshot directory’s name.
Code language: C# (cs)DirectoryInfo screenshotDirectory = Directory.CreateDirectory(screenshotPath); ScreenCapture.CaptureScreenshot(Path.Combine(screenshotDirectory.FullName, screenshotName));
Then we call Directory.CreateDirectory to create the target directory, and then proceed to generate a screenshot on the next line.
Adding a Timestamp to a Screenshot
The ScreenCapture.CaptureScreenshot method overwrites the screenshot file if the newly taken screenshot has the same name as the one that already exists.
To prevent this, let’s add a timestamp to the screenshots we create. This can be done easily with the help of the DateTime class.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
public class ScreenshotManager : MonoBehaviour
{
private string directoryName = "Screenshots";
private string screenshotName = "TestImage";
public void TakeScreenshot()
{
string timeNow = DateTime.Now.ToString("dd-MMMM-yyyy HHmmss");
ScreenCapture.CaptureScreenshot("Screenshot " + timeNow + ".png");
}
}
Code language: C# (cs)
For more information about date and time formatting, visit the official documentation.
Conclusion
Saving screenshots can take quite a bit of effort to get it right. I hope you learned something new today. Use what you’ve learned here and adjust it to suit your needs. You can even use these techniques to generate in-game screenshots to use for promoting your game.
Cheers!
Attribution
Camera icon made by Ilham Fitrotul Hayat from www.flaticon.com