/// <summary>
/// 针对相机的截图
/// </summary>
public void CameraScreenShot(){
Rect rect = new Rect(0,0,Screen.width,Screen.height);
RenderTexture renderTexture = RenderTexture.GetTemporary((int)rect.width,(int)rect.height,0, RenderTextureFormat.ARGB32);
#region 默认内置渲染管线或者urp渲染管线(相机都是Base模式),多个相机截图的顺序是depth越小越在前渲染,如下,最终就是两个相机的画面结合
// Camera.main.targetTexture = renderTexture;
// Camera.main.Render();
// camera.targetTexture = renderTexture;
// camera.Render();
#endregion
#region urp渲染管线(base和overlay两种相机) 多个相机截图的顺序是先渲染overlay的相机再是Base的相机。如下,最终就是两个相机的画面结合
// camera.targetTexture = renderTexture;
// camera.Render();
Camera.main.targetTexture = renderTexture;
Camera.main.Render();
#endregion
RenderTexture.active = renderTexture;
Texture2D texture2D = new Texture2D((int)rect.width,(int)rect.height,TextureFormat.ARGB32,false);
texture2D.ReadPixels(rect,0,0);
texture2D.Apply(false);
Camera.main.targetTexture = null;
camera.targetTexture = null;
RenderTexture.active = null;
image.sprite = Sprite.Create(texture2D,rect,new Vector2(0.5f,0.5f));
RenderTexture.ReleaseTemporary(renderTexture);
}
在这里只是记录一下多相机截图并不包含保存、加入文字水印等方式