1.application.capturescreenshot ("Screenshot.png", 0);
2.
- Exture2d CaptureScreenshot2 (rect rect)
- {
- Create an empty texture first, size can be set according to the implementation needs
- Texture2d screenshot = new texture2d ((int) rect.width, (int) Rect.height, Textureformat.rgb24,false);
- Reads screen pixel information and stores it as texture data.
- Screenshot.readpixels (rect, 0, 0);
- Screenshot.apply ();
- These texture data are then converted into a PNG image file
- byte [] bytes = Screenshot.encodetopng ();
- string filename = application.datapath + "/screenshot.png";
- System.IO.File.WriteAllBytes (filename, bytes);
- Debug.Log (string. Format ("Screenshot of a picture: {0}", filename);
- Finally, I return to this Texture2d object, so that we directly, the diagram in the game, of course, this according to their own needs.
- return screenshot;
- }
- 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.
- Texture2d Capturecamera (camera camera, rect rect)
- {
- Create a Rendertexture object
- Rendertexture RT = New rendertexture ((int) rect.width, (int) rect.height, 0);
- Temporarily set the targettexture of the relevant camera to RT and manually render the relevant camera
- Camera.targettexture = RT;
- Camera. Render ();
- PS:---If this adds a second camera, you can implement an image that is visible only to a few of the specified cameras.
- Ps:camera2.targetTexture = RT;
- Ps:camera2. Render ();
- PS:-------------------------------------------------------------------
- Activates the RT and reads the pixels from it.
- rendertexture.active = RT;
- Texture2d screenshot = new texture2d ((int) rect.width, (int) Rect.height, Textureformat.rgb24,false);
- Screenshot.readpixels (rect, 0, 0);//Note: At this time, it is reading pixels from the rendertexture.active
- Screenshot.apply ();
- Reset the relevant parameters to use the camera to continue to display on the screen
- Camera.targettexture = null;
- Ps:camera2.targetTexture = null;
- Rendertexture.active = null; jc:added to avoid errors
- Gameobject.destroy (RT);
- Finally, these texture data into a PNG image file
- byte [] bytes = Screenshot.encodetopng ();
- string filename = application.datapath + "/screenshot.png";
- System.IO.File.WriteAllBytes (filename, bytes);
- Debug.Log (string. Format ("Screenshot of a picture: {0}", filename);
- return screenshot;
- }
The Unity3d