In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces what are the screenshot methods in Unity3d, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.
Here are three screenshot methods I summarized in U3D:
1. Use the CaptureScreenshot method under the Application class.
Void CaptureScreen () {Application.CaptureScreenshot ("Screenshot.png", 0);}
This method captures a picture of the whole game at a certain frame, or a full-screen screenshot.
A. You can't take screenshots of a certain camera (camera).
B. It is not convenient and inefficient to take screenshots of partial images, so it is not recommended to use them in projects:
Although the CaptureScreenshot method itself does not achieve this point. But we can take the curve to save the nation to achieve it. The idea is like this: you can first use this method to take a full screen shot, and then get the screenshot through the path; then, through the relevant graphics class, get the local area of the screenshot and save it, so you can get a partial screenshot. I won't realize it here, but if you're interested, you can try it. It's definitely achievable.
2. The method of this second screenshot is to use the relevant methods under the Texture2d class to realize the screenshot function.
/ Captures the screenshot2. / The screenshot2. / Rect. In the screenshot area, the lower left corner is o-point Texture2D CaptureScreenshot2 (Rect rect) {/ / create an empty texture first. The size can be set as Texture2D screenShot = new Texture2D ((int) rect.width, (int) rect.height, TextureFormat.RGB24,false); / / the screen pixel information is read and stored as texture data, screenShot.ReadPixels (rect, 0,0); screenShot.Apply () / / then convert the 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 ("Screenshot: {0}", filename)) / / finally, I return the Texture2d object, so we directly, so the screenshot is shown in the game, of course, according to our own needs. Return screenShot;}
Take a full screen capture (as shown below, note: ui):
CaptureScreenshot2 (new Rect (Screen.width*0f, Screen.height*0f, Screen.width*1f, Screen.height*1f))
Cut one-fourth of the middle (as shown below):
CaptureScreenshot2 (new Rect (Screen.width*0.25f, Screen.height*0.25f, Screen.width*0.5f, Screen.height*0.5f))
Several Texture2d class methods are used here, and there are also some points to pay attention to. See for yourself.
Of course, this method does not include the ability to take screenshots for a particular camera. But the key interface has already appeared, it is Texture2d.ReadPixels (), let's not talk about this section, and move on!
3. This third method, which is the best, can take a screenshot for a certain camera.
In this way, I can capture what my Avatar sees in the scene in the game, and the UI interface (displayed with a special camera) should not have. To do this, we should separate a camera to display the ui interface and another camera camera to display 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. A new class used here is RenderTexture.
The code is as follows:
/ / A pair of camera screenshots. / The screenshot2. / Camera. The camera to be screenshot / Rect. Screenshot area 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 related camera to rt, and manually render the related camera camera.targetTexture = rt; camera.Render () / / ps:-if you add a second camera in this way, you can capture only the images seen together by a few specified cameras. / / ps: camera2.targetTexture = rt; / / ps: camera2.Render (); / / ps:-/ activate the rt and read 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 reads the pixel screenShot.Apply () from RenderTexture.active; / / resets the relevant parameters to continue to display camera.targetTexture = null; / / ps: camera2.targetTexture = null on the screen using camera RenderTexture.active = null; / / JC: added to avoid errors GameObject.Destroy (rt); / / finally, the texture data is transformed 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: {0}", filename)) Return screenShot;}
More I will not say, the relevant knowledge to find their own information, because I do not understand!
It goes straight to the picture.
Full screen picture without ui:
Only a full-screen picture of ui:
There is a full screen picture of the scene with ui (only these two cameras are specified. The relevant hints are in the "/ / ps" of the code):
Thank you for reading this article carefully. I hope the article "what are the screenshot methods in Unity3d" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.