ReadPixelsは、描画フレーム内ではなく、システムフレームバッファーからピクセルを読み取るために呼び出されました。



Readpixels Was Called Read Pixels From System Frame Buffer



UNITYスクリーンショット画面を使用する場合、直接使用する場合

// Create an empty texture first, the size can be set according to the implementation needs Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false) // Read screen pixel information and store it as texture data, screenShot.ReadPixels(rect, 0, 0) screenShot.Apply()

エラーを報告します
2つの解決策があります
1つはコルーチンを使用することです



yield return new WaitForEndOfFrame() Rect rect = new Rect(0, 0, Screen.width, Screen.height) // Create an empty texture first, the size can be set according to the implementation needs Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false) // Read screen pixel information and store it as texture data, screenShot.ReadPixels(rect, 0, 0) screenShot.Apply() // Then make these texture data into a png image file byte[] bytes = screenShot.EncodeToPNG()

1つはOnPostRenderで処理されます

bool isScreenShot = false private void OnPostRender() { if (isScreenShot) { Rect rect = new Rect(0, 0, Screen.width, Screen.height) // Create an empty texture first, the size can be set according to the implementation needs Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false) // Read screen pixel information and store it as texture data, screenShot.ReadPixels(rect, 0, 0) screenShot.Apply() // Then make these texture data into a png image file byte[] bytes = screenShot.EncodeToPNG() print('isScreenShot '+ bytes.Length) isScreenShot = false } }

このエラーは、ReadPixelsがフレーム内ではなく、システムバッファフレームからピクセルを探していることを意味します。
簡単に言えば、ダイアグラムはカメラでレンダリングされ、ReadPixelsの前にバッファーに保存される必要があります。
したがって、変更方法は、UpdateをOnPostRenderに置き換えることです。この機能はカメラの下でのみ実行され、カメラはフレームごとに実行されます。更新後に実行されます。