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);
GetPhoto();
}
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.
if I were a frog, would this post make me jump? I think … maybe 5e
Hi there, You have performed a great job. I will certainly digg it and in my opinion recommend to my friends. I am confident they will be benefited from this site. Game online PC
Great post, you have pointed out some good details , I too conceive this s a very wonderful website. 안전놀이터
Hello! I want to give you a enormous thumbs up for that fantastic information you might have here on this post. We are coming back to your blog for much more soon. 토토사이트
Doesnt work for me, the screenshot is never taken and even if i create an image on a precise path it's still don"t apply to the sprite, i must have forgotten something, could you make the projet file avalable ?
Thank you very much.