1. 程式人生 > >unity 相機截圖

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;
  }