Now you can Subscribe using RSS

Submit your Email


How to take screenshot and display in game using Unity for Android

Vishal Shrestha
In this post we will learn how to take screenshot of the game in Unity after the player has died or at any time during the game according to user input. Most of the games these days are doing it and people can see the screenshots in the game and get the option to share the screenshot to social media like Facebook, Twitter and more. Today we will discuss the most simple way to take screenshots of the game in Unity and display it on the screen.

If you are interested in sharing the screenshot to Social Media, here's part 2: Share Screenshot.

Here is how Hill Climb Racing uses screenshot.


Take screenshot of game using Unity

We will use the method CaptureScreenshot to take the screenshot. Taking screenshot using this way is very easy but the difficult part is making sure the screenshot has been saved before you display it. If you directly display the screenshot after the CaptureScreenshot method, it's highly likely that the screenshot hasn't been saved yet so you won't have a screenshot to display. The easiest way I've found is using yield. Let's take a look at the code below:

private IEnumerator takeScreenshot(){
        Application.CaptureScreenshot("screenshotmygame.png", 0);
        //So that the screenshot is taken
        yield return new WaitForEndOfFrame ();
        yield return new WaitForSecondsRealtime(1.5f);

StartCoroutine (takeScreenshot ()); 

In the code above we have created a method called takeScreenshot that takes a screenshot using the first line. The first parameter is the name as which the screenshot image will be saved and the second parameter is the factor by which to increase the resolution. I've noticed that keeping the resolution factor just 0 or 1 is find as most of the devices now are already high resolution and the greater the scale the longer it takes to save the screenshot. The screenshots taken at 0 or 1 are just fine for sharing to social media.
In the yield statements that follow the CaptureScreenshot, the first statement waits for the current frame to end before running the next statement. This is supposed to work and the screenshot should be saved by the time frame has ended but that's not always the case, hence the WaitForSecondsRealime Method. The parameter of this method defines for delay after which the following statements should be executed. I've set it to 1.5f, it should be fine for most devices and worked will all the devices I tested. 1.5 seconds should be ample time for the screenshot to save.
The StartCoroutine methods just starts an IEnumerator Method as a Coroutine, i.e the methods is executed in parallel.
Now that we have successfully saved our screenshot, let's take a look at how to get it and display it in the game display.

Display the screenshot

Displaying the screenshot  is also fairly easy. We just need to provide the path of the screenshot, read the file, load it into a texture and override the sprite of Image component. Let's take a look at the code below.
NOTE: Please note that using this approach there is an UI Image component already in the game that set to unactive. It need to be set active before overriding its sprite. In the code below it's finalScreenShot.

public void GetPhoto()
        string url = Application.persistentDataPath + "/"+ "screenshotmygame.png";
        var bytes = File.ReadAllBytes(url);
        Texture2D texture = new Texture2D (2, 2);
        bool imageLoadSuccess = texture.LoadImage(bytes);
        while (!imageLoadSuccess) {
            print ("image load failed");
            bytes = File.ReadAllBytes(url);
            imageLoadSuccess = texture.LoadImage (bytes);
        print ("Image load success: " + imageLoadSuccess);
        finalScreenShot.GetComponent<Image> ().overrideSprite = Sprite.Create (texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0f, 0f), 100f);

We have created a method call GetPhoto() that can be called for displaying the screenshot. the first method is just creating a url for the screenshot to be accessed. We are concatenating the path for game's file with the image name that we created in the first step. Then we real the image file byte by byte. Then we are loading the bytes in a texture. If the loading is failed we try it until it's successful and finally when the image is loaded successfully we just create a sprite from the texture and set it as the sprite of our UI image component. That's it! We have successfully captured the screenshot and then displayed it in our game. Try it out and if you have any questions feel free to comment below. Happy coding! :)
In the next part we will discuss how to share this screenshot to social media.

Vishal Shrestha / Author & Founder

A developer by profession, a born Adventurer. I mainly do Android but like to get my hands dirty with web development and a little bit of Python. I would't rather go on a Trek than a party and you can find me having a few rounds with the heavy bag to let out the steam ;)

For Business info : My Portfolio Site.


Post a Comment

Coprights @ 2017 | The Code City by Vishal Shrestha Vishal Shrestha