renderTexture | 将使用屏幕内容填充的 RenderTexture。 |
将此游戏对象的截屏捕获到 RenderTexture 对象中。
通过屏幕捕获的这种变体,可以使用 AsyncGPUReadback 异步读取像素,从而使该过程在主线程上花费的时间更少。
有关何时调用此方法的信息,请参阅 ScreenCapture.CaptureScreenshotAsTexture 中的说明。
using UnityEngine; using System.Collections; using UnityEngine.Rendering;
public class ScreenCaptureIntoRenderTexture : MonoBehaviour { private RenderTexture renderTexture;
IEnumerator Start() { yield return new WaitForEndOfFrame();
renderTexture = new RenderTexture(Screen.width, Screen.height, 0); ScreenCapture.CaptureScreenshotIntoRenderTexture(renderTexture); AsyncGPUReadback.Request(renderTexture, 0, TextureFormat.RGBA32, ReadbackCompleted); }
void ReadbackCompleted(AsyncGPUReadbackRequest request) { // Render texture no longer needed, it has been read back. DestroyImmediate(renderTexture);
using (var imageBytes = request.GetData<byte>()) { // do something with the pixel data. } } }