/// <summary> /// カメラ映像をスクリーンショットとして保存する /// </summary> private async UniTask TakeScreenShotAsync(CancellationToken ct) { _camera.enabled = true; await UniTask.NextFrame(cancellationToken : ct); _camera.enabled = false; // GPUからのデータ取得を待つ // AsyncGPUReadbackRequest 型が戻ってくる var req = await AsyncGPUReadback.Request(_renderTexture, 0) .WithCancellation(ct); var rawByteArray = req.GetData <byte>(); // RenderTextureの内容をPNGに変換 var png = ImageConversion.EncodeNativeArrayToPNG( rawByteArray, _renderTexture.graphicsFormat, (uint)_renderTexture.width, (uint)_renderTexture.height); var bytes = png.ToArray(); png.Dispose(); await UniTask.Run(() => File.WriteAllBytes("screenshot.png", bytes)); }
void Update() { if (!Input.GetMouseButtonDown(0)) { return; } var src = _video.texture; AsyncGPUReadback.Request(src, 0, (AsyncGPUReadbackRequest req) => { var bytes = ImageConversion.EncodeNativeArrayToPNG (req.GetData <byte>(), src.graphicsFormat, (uint)src.width, (uint)src.height, (uint)src.width * 4); File.WriteAllBytes("Frame.png", bytes.ToArray()); }); }