Screenshot of Unity3d

Source: Internet
Author: User

1.application.capturescreenshot ("Screenshot.png", 0);

2.

  1. Exture2d CaptureScreenshot2 (rect rect)
  2. {
  3. Create an empty texture first, size can be set according to the implementation needs
  4. Texture2d screenshot = new texture2d ((int) rect.width, (int) Rect.height, Textureformat.rgb24,false);
  5. Reads screen pixel information and stores it as texture data.
  6. Screenshot.readpixels (rect, 0, 0);
  7. Screenshot.apply ();
  8. These texture data are then converted into a PNG image file
  9. byte [] bytes = Screenshot.encodetopng ();
  10. string filename = application.datapath + "/screenshot.png";
  11. System.IO.File.WriteAllBytes (filename, bytes);
  12. Debug.Log (string. Format ("Screenshot of a picture: {0}", filename);
  13. Finally, I return to this Texture2d object, so that we directly, the diagram in the game, of course, this according to their own needs.
  14. return screenshot;
  15. }
  16. 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));

3.

  1. Texture2d Capturecamera (camera camera, rect rect)
  2. {
  3. Create a Rendertexture object
  4. Rendertexture RT = New rendertexture ((int) rect.width, (int) rect.height, 0);
  5. Temporarily set the targettexture of the relevant camera to RT and manually render the relevant camera
  6. Camera.targettexture = RT;
  7. Camera. Render ();
  8. PS:---If this adds a second camera, you can implement an image that is visible only to a few of the specified cameras.
  9. Ps:camera2.targetTexture = RT;
  10. Ps:camera2. Render ();
  11. PS:-------------------------------------------------------------------
  12. Activates the RT and reads the pixels from it.
  13. rendertexture.active = RT;
  14. Texture2d screenshot = new texture2d ((int) rect.width, (int) Rect.height, Textureformat.rgb24,false);
  15. Screenshot.readpixels (rect, 0, 0);//Note: At this time, it is reading pixels from the rendertexture.active
  16. Screenshot.apply ();
  17. Reset the relevant parameters to use the camera to continue to display on the screen
  18. Camera.targettexture = null;
  19. Ps:camera2.targetTexture = null;
  20. Rendertexture.active = null; jc:added to avoid errors
  21. Gameobject.destroy (RT);
  22. Finally, these texture data into a PNG image file
  23. byte [] bytes = Screenshot.encodetopng ();
  24. string filename = application.datapath + "/screenshot.png";
  25. System.IO.File.WriteAllBytes (filename, bytes);
  26. Debug.Log (string. Format ("Screenshot of a picture: {0}", filename);
  27. return screenshot;
  28. }

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.