Here are my summary, in the u3d, three kinds of screenshot method:
1, use the Capturescreenshot method under the application class.
void Capturescreen () { application.capturescreenshot ("screenshot.png"0 );}
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:
2, the second method is to use the Texture2d class of related methods, but also to implement the function.
/// <summary>///Captures the Screenshot2./// </summary>/// <returns>The screenshot2.</returns>/// <param name= "rect" >Rect. Area, bottom left corner is O point</param>texture2d CaptureScreenshot2 (rect rect) {//create an empty texture first, size can be set according to the implementation needsTexture2d screenshot =NewTexture2d ((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 (); stringfilename = 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. returnscreenshot;}
Truncate fullscreen (for example, note: There is a UI):
CaptureScreenshot2 (New Rect (screen.width*0f, screen.height*0f, screen.width*1f, screen.height*1f));
Intercept Middle 4 (e.g.): CaptureScreenshot2 (New Rect (screen.width*0.25f, screen.height*0.25f, screen.width*0.5f, screen.height* 0.5f));
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:
/// <summary>///to the camera. /// </summary>/// <returns>The screenshot2.</returns>/// <param name= "Camera" >camera. Cameras to be snapped</param>/// <param name= "rect" >Rect. Area of the screenshot</param>texture2d Capturecamera (camera camera, rect rect) {//Create a Rendertexture objectRendertexture RT =NewRendertexture ((int) Rect.width, (int) Rect.height,0); //temporarily set the targettexture of the relevant camera to RT and manually render the relevant cameraCamera.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=NewTexture2d ((int) Rect.width, (int) Rect.height, Textureformat.rgb24,false); Screenshot.readpixels (Rect,0,0);//Note: This time, it is reading pixels from the rendertexture.activescreenshot.apply (); //Reset the relevant parameters to use the camera to continue to display on the screenCamera.targettexture =NULL; //ps:camera2.targetTexture = null;Rendertexture.active =NULL;//jc:added to avoid errorsGameobject.destroy (RT); //Finally, these texture data into a PNG image file byte[] bytes =screenshot.encodetopng (); stringfilename = Application.datapath +"/screenshot.png"; System.IO.File.WriteAllBytes (filename, bytes); Debug.Log (string. Format ("screenshot of a picture: {0}", filename)); returnscreenshot;}
Reprint please note the source at the beginning of the text: http://blog.csdn.net/anyuanlzh/article/details/17008909
3 Ways to Unity3d
(go)