当前活动的渲染纹理。
所有渲染都进入活动 RenderTexture。如果活动 RenderTexture 为 null
,则所有内容都渲染到主窗口。
设置 RenderTexture.active 等同于调用 Graphics.SetRenderTarget。通常,在实现自定义图形效果时,您会更改或查询活动渲染纹理;如果您只需要让摄像机渲染到纹理,则使用 Camera.targetTexture。
当 RenderTexture 变得活跃时,如果它尚未创建,则会自动创建其硬件渲染上下文。
其他资源:Graphics.SetRenderTarget、GraphicsTexture.active。
using UnityEngine; using System.Collections;
// Get the contents of a RenderTexture into a Texture2D public class ExampleClass : MonoBehaviour { static public Texture2D GetRTPixels(RenderTexture rt) { // Remember currently active render texture RenderTexture currentActiveRT = RenderTexture.active;
// Set the supplied RenderTexture as the active one RenderTexture.active = rt;
// Create a new Texture2D and read the RenderTexture image into it Texture2D tex = new Texture2D(rt.width, rt.height); tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0); tex.Apply();
// Restore previously active render texture RenderTexture.active = currentActiveRT; return tex; } }