This is the 10th day of my participation in the August Text Challenge.More challenges in August

An overview of the

During the development process, there is often a function to save images, so how do these images save? Some direct is a picture, save it directly, and have a plenty of a whole screen picture, and how to save it? This article will take a detailed look at how to capture and save images using the screenshot function in Unity. There are several ways to save images in Unity:

  1. Print screen
  2. Custom location and size screenshot
  3. Custom camera and size screenshots

Next, let’s take a screenshot of the following screen

Function implementation

Print screen

Print screen we are using the ScreenCapture. CaptureScreenshot an advantage of using the print screen is: simple, fast, the interception of a picture frame, to all the main camera can see in the picture all image capture. The disadvantage is that the image size can not be specified, only full screen capture. The interception method is as follows:

ScreenCapture. CaptureScreenshot (Application. StreamingAssetsPath + / print screen. PNG, 0).Copy the code

The CaptureScreenshot parameter 1 is the path to save the image and the name of the image

The result of running the above method is

Custom location and size screenshot

Custom screenshots we use Texture2d. ReadPixels, which capture a specified range of screens according to a Rect type and store the screen pixels as textured images. The advantage of this method is that the position of the screenshot and the size of the picture can be arbitrarily defined (be careful not to go beyond the screen scope →_→). When taking a screenshot, we use the IEnumerator, which needs to be executed after the camera renders a frame, so we need to use the coroutine

Yield Return New WaitForEndOfFrame();Copy the code

Next we initialize a Texture2D, which can be changed to a custom size as required

Texture2D mTexture = new Texture2D((int)mRect.width, (int)mRect.height,TextureFormat.RGB24, false);
Copy the code

Once Texture2D is defined, screen pixel information is read and stored as texture data based on our defined position and size

mTexture.ReadPixels(mRect, 0, 0); / / application mTexture. The Apply ();Copy the code

Finally, save the screenshot file

Byte [] bytes = mtexture.encodeTopng (); / / save System. IO. File. WriteAllBytes (Application. StreamingAssetsPath + / custom screenshots. PNG, bytes);Copy the code

IEnumerator CaptureByRect(Rect mRect, string mFileName) where mRect is the location and size information that we define. In Unity, for example: new Rect(100, 100, 1024, 768), note that this method defines a custom position. The initial position (0,0) is the small left corner of the screen, so the first two digits of Rect are our custom starting position, and the last two digits are the image size. Next, new Rect(100, 100, 1024, 768) was used to capture the screenshot. The screenshot area is shown below

The final screenshot is as follows

Custom camera and size screenshots

Custom camera screenshots, using RenderTextures, different from Texture2D.ReadPixels. RenderTextures reads pixels of a camera’s rendering. The steps to use custom camera screenshots are similar to the previous method, the only difference is that you need to set up the camera render and then read the screenshots and save them. The nice thing about using RenderTextures is that you can specify the camera when you take a screenshot, so you can mask the UI when you take a screenshot. Again, the first thing to do is wait until a frame has been rendered

Yield Return New WaitForEndOfFrame();Copy the code

Initialize a RenderTexture, assign it to the Camera, and activate the RenderTexture

RenderTexture mRender = new RenderTexture((int) mrect.width, (int) mrect.height, 16); RenderTexture mRender = new RenderTexture((int) mrect.width, (int) mrect.height, 16); mCamera.targetTexture = mRender; // start rendering McAmera.render (); RenderTexture.active = mRender;Copy the code

Define Texture and read pixel data

Texture2D mTexture = new Texture2D((int)mRect.width, (int)mRect.height, TextureFormat.RGB24, false); // Read screen pixel information and store it as texture data mtexture.readPixels (mRect, 0, 0); / / application mTexture. The Apply ();Copy the code

After reading the data, one more step is done, which is to release the resource, to release the rendered data just destroyed

mCamera.targetTexture = null;
RenderTexture.active = null;
GameObject.Destroy(mRender);
Copy the code

Finally, the method of saving data is the same as above, and the final captured picture is as follows

conclusion

This paper introduces three methods of screen capture, each with its own advantages and disadvantages

  1. Print screen (ScreenCapture CaptureScreenshot) at the time of print screen is very convenient.
  2. Custom screenshots (Texture2d.readPixels) are ideal for customizing image sizes.
  3. When taking a screenshot, you can specify the camera, which is suitable to screen the UI.

Write in the last

All the shared content is the author used in the daily development process of a variety of small function points, sharing is also a way to review, if there is a bad place to write, please give more advice. Demo source code will be sorted out and shared with you later. Welcome to learn from each other and make progress.