SoFunction
Updated on 2025-03-07

Unity implements screenshots and screenshots based on camera screenshots

In game development and software development, screenshot functions are often needed, including screenshots with UI and screenshots without UI. The code is as follows:

using ;
using ;
using UnityEngine;
 
public static class ScreenShotForCamera{
 
 public static void CaptureScreen(string _path = null)
 {
 if (_path == null)
 _path = "";
 
 (_path, 0);
 }
 
 public static Texture2D CaptureScreen(Rect rect, bool _isCreatePhoto = false, string _path = null)
 {
 // Create an empty texture first, the size can be set according to the implementation needs Texture2D screenShot = new Texture2D((int), (int), TextureFormat.RGB24, false);
 
 // Read screen pixel information and store it as texture data. (rect, 0, 0);
 ();
 
 // Then turn these texture data into a png image file if (_isCreatePhoto)
 {
 if(_path == null)
 _path =  + "/";
 
 byte[] bytes = ();
 string filename = _path;
 (filename, bytes);
 (("Take a screenshot: {0}", filename));
 }
 
 // Finally, I return this Texture2d object, so we directly, so this screenshot is shown in the game, of course this is based on my own needs. return screenShot;
 }
 
 //
 public static Texture2D CaptureCamera(ref Camera _camera, Rect _rect, int _destX, int _destY, bool _isCreatePhoto = false, string _path = null)
 {
 RenderTexture renderTexture = new RenderTexture((int)_rect.width, (int)_rect.height, 24, RenderTextureFormat.ARGB32);
 _camera.targetTexture = renderTexture;
 _camera.Render();
 
 // Activate this renderTexture and read pixels from it  = _camera.targetTexture;
 Texture2D screenShot = new Texture2D((int)_rect.width, (int)_rect.height, TextureFormat.ARGB32, false);
 (_rect, _destX, _destY); //Read the image of _rect size from the coordinates (_destX,_destY) ();
 
 //Reset parameters //_camera.targetTexture = null;
  = null;
 //(renderTexture);
 
 //Generate PNG pictures if (_isCreatePhoto)
 {
 if (_path == null)
 _path =  + "/";
 
 byte[] bytes = ();
 string filename = _path;
 (filename, bytes);
 (("Take a screenshot: {0}", filename));
 }
 
 return screenShot;
 }
}

The editor will share with you another paragraph: Unity implements screenshot function, hoping it can help you

public class ScreenShot : MonoBehaviour 
{

 void OnScreenShotClick()
 {
 //Get the current system time  now = ;
 string times = ();
 //Remove the spaces before and after times = ();
 //Replace the slash with horizontal bar times = ("/", "-");

 string fileName = "ARScreenShot" + times + ".png";
 //Judge whether the platform is an Android platform if ( == )
 {
 //The parameters are screen width screen height texture format whether to use mapping Texture2D texture = new Texture2D(, , TextureFormat.RGB24, false);
 //Read the map (new Rect(0, 0, , ), 0, 0);
 //App screenshot ();
 //Serialize the object byte[] bytes = ();
 //Set the path of the mobile phone folder to be stored string destination = "/sdcard/DCIM/Screenshots";
 //If the folder does not exist if (!(destination))
 {
 //Create this folder (destination);
 }
 string pathSave = destination + "/" + fileName;
 (pathSave, bytes);
 }
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.