Screenshot
A picture is worth a thousand words and about 500 lines of code ... this lets you do it in 1

Introduction

1
public static class API.Screenshot
Copied!
This interface enables you to capture full screen or parts of a screen to texture, to file and to optionally scale the data to suit your needs.

What can it do?

  • Capture full screen to texture
  • Capture a rect of the screen to texture
  • Scale a texture (point or bilinear)
  • save a texture or the screen or part of the screen to disk

How To

All processes are asynchronious and require you to pass in a MoboBehaviour so that the interface can detect the end of frame insuring a proper capture of the redering.
Once complete the callback will be invoked providing you with the completed texture and a boolean indicating IO Failure if any for example
1
API.Screenshot.Capture(this, (texture, failure) =>
2
{
3
if(!failure)
4
Debug.Log("texture contains the screen shot!");
5
});
Copied!

Capture the full screen

1
API.Screenshot.Capture(component, callback);
Copied!

Capture part of the screen

From repreesnts the lower left and To represents the upper rigth of the corners of the screen rect to be taken, these are in pixel space e.g. (0,0) & (256,256) would capture a 256x256 area at the botton left corner of the screen.
1
API.Screenshot.Capture(componenet, from, to, callback);
Copied!

Save image to file

You can capture and save in a single line call to any of Unity's supported encoding types. The save image operation can also be used on Texture2D so you can save any texture you have available.
1
API.Screenshot.SaveImage(componenet,
2
path,
3
imageType,
4
scaleToResoltuion,
5
lowerLeftCorner,
6
upperRightCorner,
7
callback);
Copied!
All paramiters except the componenet paramiter are optional, that is you could capture a screen shot with as little code as
1
API.Screenshot.SaveImage(componenet);
Copied!
The result is that the full screen would be captured at full resolution and saved to the game's persistant data storage path as a JPG with the name screenshot_20210113133230.jpg note the format of the file name is simply screenshot followed by the current date time in the form yyyyMMddHHmmss this should insure the file does not overwrite unless you managed to call this method twice within one second.
In most use cases you actually want to save a scaled down version of the screenshot such as for a thumbnail in a save file. To do that you would use
1
API.Screenshot.SaveImage(componenet,
2
path,
3
default,
4
new Vector2Int(256, 144),
5
default,
6
default,
7
callback);
Copied!
By passing default in we ask the method to use its defaults e.g. JPG and to capture the full screen. In this example we chose to speicify the path, resolution and a callback. The callback will give us the texture 2D so we can show it to screen for the user, it will also tell us the final path that was saved to and rather or not there where any IO errors.

Scale a texture

The screenshot interfaces Point and Bilinear scale operations are exposed for your use, this can be useful when creating other thumbnail like images. There use is simple and strait forward
1
API.Screenshot.BiliniearScale(texture, width, height);
Copied!
The texture will be resized with its contnet adjusted to scale into the width and height provided.
Last modified 7mo ago