source | 要从中读取的渲染目标区域。 |
destX | 要在纹理中写入像素的 x 位置。 |
destY | 要在纹理中写入像素的 y 位置。 |
recalculateMipMaps | 当值为 true 时,Unity 会在写入像素数据后自动重新计算纹理的 mipmap。否则,Unity 不会自动执行此操作。 |
从当前渲染目标读取像素并将其写入纹理。
此方法从 GPU 上当前活动的渲染目标(例如屏幕、RenderTexture 或 GraphicsTexture)复制一个矩形区域的像素颜色,并将它们写入 CPU 上位于位置 (destX
, destY
) 的纹理。 Texture.isReadable 必须为 true
,并且必须在 ReadPixels
后调用 Apply 以将更改后的像素上传到 GPU。
左下角为 (0, 0)。ReadPixels
通常很慢,因为此方法会首先等待 GPU 完成所有先前的工作。要更快地复制纹理,请改用以下方法之一
渲染目标和纹理必须使用相同的格式,并且该格式必须在设备上同时支持渲染和采样。
您可以在调用 Apply 时自动更新 mipmap,而不是将 recalculateMipMaps
设置为 true
。
以下代码示例演示了如何在内置渲染管线中使用 ReadPixels
。在可脚本化渲染管线(如通用渲染管线 (URP))中,Camera.onPostRender 不可用,但您可以以类似的方式使用 RenderPipelineManager.endCameraRendering。
using UnityEngine;
public class ReadPixelsExample : MonoBehaviour { // Set Renderer to a GameObject that has a Renderer component and a material that displays a texture public Renderer screenGrabRenderer;
private Texture2D destinationTexture; private bool isPerformingScreenGrab;
void Start() { // Create a new Texture2D with the width and height of the screen, and cache it for reuse destinationTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
// Make screenGrabRenderer display the texture. screenGrabRenderer.material.mainTexture = destinationTexture;
// Add the onPostRender callback Camera.onPostRender += OnPostRenderCallback; }
void Update() { // When the user presses the Space key, perform the screen grab operation if (Input.GetKeyDown(KeyCode.Space)) { isPerformingScreenGrab = true; } }
void OnPostRenderCallback(Camera cam) { if (isPerformingScreenGrab) { // Check whether the Camera that just finished rendering is the one you want to take a screen grab from if (cam == Camera.main) { // Define the parameters for the ReadPixels operation Rect regionToReadFrom = new Rect(0, 0, Screen.width, Screen.height); int xPosToWriteTo = 0; int yPosToWriteTo = 0; bool updateMipMapsAutomatically = false;
// Copy the pixels from the Camera's render target to the texture destinationTexture.ReadPixels(regionToReadFrom, xPosToWriteTo, yPosToWriteTo, updateMipMapsAutomatically);
// Upload texture data to the GPU, so the GPU renders the updated texture // Note: This method is costly, and you should call it only when you need to // If you do not intend to render the updated texture, there is no need to call this method at this point destinationTexture.Apply();
// Reset the isPerformingScreenGrab state isPerformingScreenGrab = false; } } }
// Remove the onPostRender callback void OnDestroy() { Camera.onPostRender -= OnPostRenderCallback; } }