Here are my summary, in the u3d, three kinds of screenshot method:
1, use the Capturescreenshot method under the application class.
[CSharp]View Plaincopy
- 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:
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
- <summary>
- Captures the Screenshot2.
- </summary>
- <returns>the screenshot2.</returns>
- <param name= "Rect" >rect, the lower left corner is O point </param>
- Texture2d 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);
- //Read screen pixel information and store it as texture data.
- Screenshot.readpixels (rect, 0, 0);
- Screenshot.apply ();
- //Then put 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);
- ///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.
- 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));
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
- <summary>
- to the camera.
- </summary>
- <returns>the screenshot2.</returns>
- <param name= "Camera" >camera. Camera to be snapped </param>
- <param name= "rect" >rect. Area of the screenshot </param>
- 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 render the relevant camera manually
- 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 this 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: This time, it is reading pixels from the rendertexture.active
- Screenshot.apply ();
- //Reset related parameters to use camera to continue to display on screen
- Camera.targettexture = null;
- //ps:camera2.targettexture = null;
- Rendertexture.active = null; //jc:added to avoid errors
- Gameobject.destroy (RT);
- //end up with 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;
- }
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