unity 相机截屏

1.指定相机截屏

  public Texture2D CaptureScreen(Camera came, Rect rect)
  {
      RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
 
      came.targetTexture = rt;
      came.Render();
 
      RenderTexture.active = rt;
      Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
 
      screenShot.ReadPixels(r, 0, 0);
      screenShot.Apply();
 
      came.targetTexture = null;
      RenderTexture.active = null;
      GameObject.Destroy(rt);
 
      byte[] bytes = screenShot.EncodeToPNG();
      string filename = Application.streamingAssetsPath + "/Shot.png";
      System.IO.File.WriteAllBytes(filename, bytes);
 
      return screenShot;
  }

猜你喜欢

转载自blog.csdn.net/u012909508/article/details/83015697