Unity screenshot method

Source: Internet
Author: User

  1. Calling the Unity API

    Screencapture. Capturescreenshot("Screen.png", 0);

    1. Images can only be saved in PNG format, and the first parameter must be added. png
    2. The second parameter is a set resolution, which increases the resolution if it is greater than 1, or 4 times times the default resolution, as 4.
    3. Can only be saved in the application.persistentdatapath directory
    4. The function itself can only intercept the full screen, if you really want to intercept part of the screen, you can be after, as a local image after reading, modify

  2. Using the Texture2d.readpixels method, this method can read data from the current rendertexture (for example, if it is not set to screen) to a texture2d
      1. Code

    Private IEnumerator CaptureScreenshot2(rect rect)

    {

    Texture2d screenshot = new texture2d((int)rect. Width, (int)rect. Height, textureformat. RGB24, false);

    Yield return new waitforendofframe();

    Screenshot. Readpixels(rect, 0, 0);

    Screenshot. Apply();

    byte[] bytes = screenshot. Encodetopng();

    string filename = application. DataPath + "/screenshot.png";

    System. IO. File. WriteAllBytes(filename, bytes);

    }

    1. You must use coroutine here, and call Readpixels after the current frame is rendered, because the image is now in the current rendertexture
    2. rect different controlled ranges

      e.g.

      startcoroutine (capturescreenshot2< Span style= "COLOR: #404040" > (new rect 00 screen Width/2 screen< Span style= "COLOR: #404040". Height/2

      Figure

    3. from the lower left corner of the screen width and height
  3. Improved version of Method 2, Method 2 Readpixels By default read rendertexture.active when there is no manual setting, this value is the screen image, here we can set the rendertexture to intercept only one camera to see the image
    1. preparation, add a camera, and the camera only renders the UI, in order not to obscure the object rendered by the first camera, here Clearflag set to don ' t Clear

      There are two cameras in this scene

    2. Code

      The idea is to create a rendertexture and render through the camera and then reload the rendertexture, read through Readpixels

      Private void capturecamera(camera camera, rect rect)

      {

      Rendertexture _rendertex = new rendertexture((int)rect. Width, (int)rect. Height, 0);

      Camera. Targettexture = _rendertex;

      Camera. Render();

      Camera. Targettexture = null;

      Rendertexture. Active = _rendertex;

              texture2d screenshot = new texture2d ( int) rect Widthint rect< Span style= "COLOR: #404040". Height TextureFormat. Rgb24false

      Screenshot. Readpixels(rect, 0, 0);

      Screenshot. Apply();

      Rendertexture. Active = null;

      byte[] bytes = screenshot. Encodetopng();

      string filename = application. DataPath + "/screenshot.png";

      System. IO. File. WriteAllBytes(filename, bytes);

      }

    3. Call this function, and pass the camera parameter to render the UI only camera, and full screen, here in order to make the results more obvious, we set the UI font to red, the result is as follows

Reference

http://blog.csdn.net/anyuanlzh/article/details/17008909

Unity method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.