Simple game screenshots_controllable screenshots 1

a need

I need to take screenshots at different levels in the scene (such as only taking screenshots of the model, only taking the UI, or only taking the externally added images, or taking all of them, or working with the Shader to present the screen switching of the thermal imaging of the human eye night vision device)
Sort the screenshots into a list and display them in the scene UI

How to do

  1. The camera should be able to see different images
  2. Store the current frame
  3. Display the stored screen

Knowledge points that need to be mastered when implementing

The camera should be able to see different images
  1. Classify different game objects into different Layers
  2. You need to set up a separate camera AudioListener, to prevent duplication)
  3. Usually we have to turn it off and only turn it on when taking screenshots
Method to dynamically control camera Rendering->CullingMask
    
    
     
     
public Camera cameraToChange ; //控制的相机 public LayerMask newCullingLayers ; //选择Layers void SetCullingMask ( ) { //显示选择的Layers cameraToChange . cullingMask = newCullingLayers ; } void ToggleCullingMask ( ) { // 取反剔除层 cameraToChange . cullingMask = ~ cameraToChange . cullingMask ; } void DisableAllLayers ( ) { //全部剔除 cameraToChange . cullingMask = 0 ; }
Save the screen
You need to enter the width and height for creating Texture2D and screenshot content. There are two ways to directly enter the screen storage or create an image. The width and height of this image are commonly used.
    
    
     
     
int width = Screen . width ; int height = Screen . height ;public RectTransform UIRect ; { //计算截图的宽度和高度 int width = ( int ) ( UIRect . rect . width ) ; int height = ( int ) ( UIRect . rect . height ) ; //创建一个新的Texture2D对象,宽度和高度与截图的宽度和高度匹配 Texture2D tex = new Texture2D ( width , height , TextureFormat . RGB24 , false ) ; }
storage method
Create the StreamingAssets folder in advance
    
    
     
     
public RectTransform UIRect ; public RawImage rawImage ; // 添加一个RawImage对象 void Update ( ) { if ( Input . GetKeyDown ( KeyCode . Q ) ) { string fileName = Application . dataPath + "/StreamingAssets/" + "12.png" ; //系统不识别标点符号,但支持中文 IEnumerator coroutine = CaptureByUI ( UIRect , fileName ) ; StartCoroutine ( coroutine ) ; } } public IEnumerator CaptureByUI ( RectTransform UIRect , string mFileName ) { yield return new WaitForEndOfFrame ( ) ; //等待当前帧的UI渲染完成 //计算截图的宽度和高度 int width = ( int ) ( UIRect . rect . width ) ; int height = ( int ) ( UIRect . rect . height ) ; //创建一个新的Texture2D对象,宽度和高度与截图的宽度和高度匹配 Texture2D tex = new Texture2D ( width , height , TextureFormat . RGB24 , false ) ; //计算从屏幕上读取像素的起始位置 float leftBtmX = UIRect . transform . position . x + UIRect . rect . xMin ; float leftBtmY = UIRect . transform . position . y + UIRect . rect . yMin ; //使用tex.ReadPixels()函数从屏幕上读取指定区域的像素,并存储到Texture2D中。 tex . ReadPixels ( new Rect ( leftBtmX , leftBtmY , width , height ) , 0 , 0 ) ; //执行读取操作,将修改应用到Texture2D中 tex . Apply ( ) ; //将Texture2D编码为PNG格式的字节数组 byte [ ] bytes = tex . EncodeToPNG ( ) ; //将字节数组保存为PNG图片文件 System . IO . File . WriteAllBytes ( mFileName , bytes ) ; }
Display the stored screen
Go to UnityWebRequest and get the data from the given path.
    
    
     
     
public RawImage rawImage ; public string imageFileName ; void Update ( ) { if ( Input . GetKeyDown ( KeyCode . W ) ) { // 拼接图片路径 string imagePath = System . IO . Path . Combine ( Application . streamingAssetsPath , imageFileName ) ; // 开始协程加载图片 StartCoroutine ( LoadImage ( imagePath ) ) ; } } IEnumerator LoadImage ( string path ) { // 发送请求获取图片 UnityWebRequest www = UnityWebRequestTexture . GetTexture ( path ) ; yield return www . SendWebRequest ( ) ; // 检查请求是否成功 if ( www . result == UnityWebRequest . Result . Success ) { // 获取加载的Texture Texture2D texture = DownloadHandlerTexture . GetContent ( www ) ; // 将加载的Texture赋值给RawImage的texture属性 rawImage . texture = texture ; // 调整RawImage的大小以适应图片的长宽比例 rawImage . SetNativeSize ( ) ; } else { Debug . LogError ( "Failed to load image: " + www . error ) ; } }

Guess you like

Origin blog.csdn.net/unityofficial/article/details/132102998