Screenshot function of Unity3d

Source: Internet
Author: User

Here are my summary, in the u3d, three kinds of screenshot method:

1, use the Capturescreenshot method under the application class.

[CSharp]View Plaincopy
    1. void Capturescreen ()
    2. {
    3. Application.capturescreenshot ("Screenshot.png", 0);
    4. }

This method captures the entire screen of the game at a certain frame, or a full screen.

A, can not be targeted at a camera (cameras) of the screen, do.

b, to the local screen, the implementation of inconvenient, low efficiency, not recommended in the project use:

Although Capturescreenshot this method, itself is not to do this. But we can take the path of the salvation of the curve to achieve it. The idea is this: you can first use this method to a full screen, and then through the path to get to this, then through the relevant graphics class, get this local area and save it, so you can get a local. I will not implement it here, but interested can try, certainly can be achieved.

2, the second method is to use the Texture2d class of related methods, but also to implement the function.

[CSharp]View Plaincopy
  1. <summary>
  2. Captures the Screenshot2.
  3. </summary>
  4. <returns>the screenshot2.</returns>
  5. <param name= "Rect" >rect, the lower left corner is O point </param>
  6. Texture2d CaptureScreenshot2 (rect rect)
  7. {
  8. //Create an empty texture first, size can be set according to the implementation needs
  9. Texture2d screenshot = new Texture2d ((int) rect.width, (int) rect.height, textureformat.rgb24,false);
  10. //Read screen pixel information and store it as texture data.
  11. Screenshot.readpixels (rect, 0, 0);
  12. Screenshot.apply ();
  13. //Then put these texture data into a PNG image file
  14. byte[] bytes = Screenshot.encodetopng ();
  15. string filename = Application.datapath + "/screenshot.png";
  16. System.IO.File.WriteAllBytes (filename, bytes);
  17. Debug.Log (string.  Format ("screenshot of a picture: {0}", filename);
  18. ///Finally, I return this Texture2d object so that we are directly, and this diagram is in the game, of course this is based on your own needs.
  19. return screenshot;
  20. }

Truncate fullscreen (for example, note: There is a UI):
CaptureScreenshot2 (New Rect (screen.width*0f, screen.height*0f, screen.width*1f, screen.height*1f));


Section 4 (e.g.):
CaptureScreenshot2 (New Rect (screen.width*0.25f, screen.height*0.25f, screen.width*0.5f, screen.height*0.5f));


There are several methods of texture2d class used here, there are some places to pay attention to, see for yourself.

Of course, this method does not have to implement the function for a camera. But the key interface has appeared, it is texture2d.readpixels (), this paragraph will not say, then look down!

3, this third method, the most cattle, can be targeted at a camera.

In this case, I can intercept, my avatar in the game scene in the picture, the UI interface (with a special camera display) what is not supposed to be. To do this, we should separate a camera to specifically display the UI interface and use another camera camera to display the scene in the scene. Then we just take a screenshot of the scene camera. So the key point is: How to take a screenshot of a camera. Here a new class is rendertexture.

The code is as follows:

[CSharp]View Plaincopy
  1. <summary>
  2. to the camera.
  3. </summary>
  4. <returns>the screenshot2.</returns>
  5. <param name= "Camera" >camera. Camera to be snapped </param>
  6. <param name= "rect" >rect. Area of the screenshot </param>
  7. Texture2d Capturecamera (camera camera, rect rect)
  8. {
  9. //Create a Rendertexture object
  10. Rendertexture RT = New Rendertexture ((int) rect.width, (int) rect.height, 0);
  11. //Temporarily set the targettexture of the relevant camera to RT and render the relevant camera manually
  12. Camera.targettexture = RT;
  13. Camera. Render ();
  14. //ps:---If this adds a second camera, you can implement an image that is visible only to a few of the specified cameras.
  15. //ps:camera2.targettexture = RT;
  16. //ps:camera2.  Render ();
  17. //ps:-------------------------------------------------------------------
  18. //activates this RT and reads the pixels from it.
  19. rendertexture.active = RT;
  20. Texture2d screenshot = new Texture2d ((int) rect.width, (int) rect.height, textureformat.rgb24,false);
  21. Screenshot.readpixels (rect, 0, 0); //Note: This time, it is reading pixels from the rendertexture.active
  22. Screenshot.apply ();
  23. //Reset related parameters to use camera to continue to display on screen
  24. Camera.targettexture = null;
  25. //ps:camera2.targettexture = null;
  26. Rendertexture.active = null; //jc:added to avoid errors
  27. Gameobject.destroy (RT);
  28. //end up with these texture data into a PNG image file
  29. byte[] bytes = Screenshot.encodetopng ();
  30. string filename = Application.datapath + "/screenshot.png";
  31. System.IO.File.WriteAllBytes (filename, bytes);
  32. Debug.Log (string.  Format ("screenshot of a picture: {0}", filename);
  33. return screenshot;
  34. }

More I will not say, the relevant knowledge to find information on their own, because I do not understand!

Straight up.

Full-screen view without UI:

Full-screen view of the UI only:

There is a full-screen view of the UI with the scene (just specify the two cameras, the relevant hints in the code "//PS"):

Reprint please note the source at the beginning of the text: http://blog.csdn.net/anyuanlzh/article/details/17008909

Features of the Unity3d

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.